我想识别具有 2 组双字母的单词(在字典结构中)。
我是 Python / regex 的新手——但我设法从网站其他地方的一些类似问题中收集了几乎存在的代码。但这并不完全奏效。
它会拾取两组双打,但前提是它们是相同的字母,如果它们是分开的,它会拾起它们。我认为 \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