[英]How to define cycles in a regex pattern?
I want to assert that a String contains two identical subStrings. 我想断言一个字符串包含两个相同的子字符串。
I tried this regex: [A-ZA-Z]{2}
我尝试过此正则表达式: [A-ZA-Z]{2}
What I want to detect: 我要检测的是:
AZAZ
CUCU
PIPI
But this regex just checks if there are 4 uppercased Letters in a row, so 但是此正则表达式仅检查连续是否有4个大写字母,因此
ABCD --> it's good and it shouldn't be
I'm really sorry if this is unclear, I don't get how to write the regex to assert repetitions. 如果还不清楚,我真的很抱歉,我不知道如何编写正则表达式来断言重复。
With Python you can do the following: 使用Python,您可以执行以下操作:
p="((?P<two_chars>[A-Z]{2})(?P=two_chars))"
s="AZAZABCDCUCUPIPI"
re.findall(p, s)
[('AZAZ', 'AZ'), ('CUCU', 'CU'), ('PIPI', 'PI')]
and then extract from the list the elements you want. 然后从列表中提取所需的元素。 Or you can be faster and do: 或者,您可以更快地执行以下操作:
[k for k,v in re.findall(p,s)]
['AZAZ', 'CUCU', 'PIPI']
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.