在perl中:
split(/(?<=[KR])/,$mystring)
通过两个概念“在每个字符之间分割”(=空字符串)“向后看”,在每个K或R之后分割mystring.因此,序列AAAKBBBBR变为(AAAK,BBBBR).
python中对应的是什么?我找不到方法,因为空字符串不会在字符之间分割!
解决方法:
您真的需要环顾四周吗?这个正则表达式应该执行[^ KR] * [KR]:
In [1]: import re # Import the regex library
In [2]: s = "AAAKBBBBR" # Define the input string
In [3]: re.findall(r'[^KR]*[KR]', s) # Find all the matches in the string
Out[3]: ['AAAK', 'BBBBR']
解释:
[^KR] # ^ in character classes is negation so will match any character except K/R
* # Quantifier used to match zero or more of the previous expression
[KR] # Simple character class matching K/R
换句话说:匹配零个或多个后跟K / R的非K / R字符.
在以下情况下,您可能希望使用量词匹配至少一个或多个而不是*:
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']
要使结尾的[KR]为可选,可以使用?:
In [5]: s = 'AAAKBBBBRAAA'
In [6]: re.findall(r'[^KR]+[KR]?', s)
Out[6]: ['AAAK', 'BBBBR', 'AAA']