我想要识别具有两组双字母的单词(在字典结构中)。
我是Python/正则表达式新手,但已经从该网站上的一些类似问题中拼凑了几乎能用的代码。但还不够完美。
它只会捕获两个相同字母的重复部分,并且只会在它们被分开时才会捕获它们。我认为第二个\1的使用是问题所在,仅适用于与第一个捕获组相同字母的情况。通过regex101确认了这一点,但不确定如何调整正则表达式以正确匹配。
任何有关我偏离正道的指针都将不胜感激。
我是Python/正则表达式新手,但已经从该网站上的一些类似问题中拼凑了几乎能用的代码。但还不够完美。
它只会捕获两个相同字母的重复部分,并且只会在它们被分开时才会捕获它们。我认为第二个\1的使用是问题所在,仅适用于与第一个捕获组相同字母的情况。通过regex101确认了这一点,但不确定如何调整正则表达式以正确匹配。
任何有关我偏离正道的指针都将不胜感激。
#logic being [any letter]* [any letter repeated] [any letter]* [any letter repeated] [any letter]*
import json
import re
dict_data = {"hello":0, "aaoo":0, "aabaa":0, "aaaba":0, "bookkeeping":0, "bookkeeooping":0}
for key in dict_data:
if re.search(r'\b.*(.)\1.*(.)\1.*\b', key):
print("Match found: ", key)
else:
print("No match: ", key)
输出结果为:
No match: hello
No match: aaoo #This should work but doesn't
Match found: aabaa #This works
Match found: aaaba #This shouldn't, assume it is matching either 2nd&3rd a or 3rd&4th a
No match: bookkeeping #This should match but doesn't
Match found: bookkeeooping #This works, assume it is matching oo twice