regex - Translate perl split to python split -
in perl:
split(/(?<=[kr])/,$mystring)
splits mystring after every k or r via 2 concepts "split between every character" (=empty string) + "lookbehind". sequence aaakbbbbr becomes (aaak,bbbbr).
whats counterpart in python? cant find way because empty string not split between characters!
do need around? regular look should [^kr]*[kr]
:
in [1]: import re # import regex library in [2]: s = "aaakbbbbr" # define input string in [3]: re.findall(r'[^kr]*[kr]', s) # find matches in string out[3]: ['aaak', 'bbbbr']
regexplanation:
[^kr] # ^ in character classes negation match character except k/r * # quantifier used match 0 or more of previous look [kr] # simple character class matching k/r
in words: match 0 or more characters not k/r followed k/r.
you might want utilize +
quantifier match @ to the lowest degree 1 or more instead of *
cases like:
in [1]: import re in [2]: s = "kaaakbbbbr" in [3]: re.findall(r'[^kr]*[kr]', s) out[3]: ['k', 'aaak', 'bbbbr'] in [4]: re.findall(r'[^kr]+[kr]', s) out[4]: ['aaak', 'bbbbr']
to create trailing [kr]
optional can utilize ?
:
in [5]: s = 'aaakbbbbraaa' in [6]: re.findall(r'[^kr]+[kr]?', s) out[6]: ['aaak', 'bbbbr', 'aaa']
python regex perl
No comments:
Post a Comment