簡體   English   中英

匹配一個單詞中的多個雙字符 - Python 正則表達式

[英]Matching multiple double characters in a word - Python regex

我想識別具有 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)

Output 是:

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

第二個\1指的是第一個捕獲組的值,而您需要使用\2引用第二個組的值。

re.search在輸入字符串的任何位置搜索正則表達式匹配,您不需要在輸入的兩端使用.*

利用

dict_data = {"hello":0, "aaoo":0, "aabaa":0, "aaaba":0, "bookkeeping":0, "bookkeeooping":0}
for key in dict_data:
    if re.search(r'(.)\1.*(.)\2', key):
        print("Match found: ", key)
    else:
        print("No match:    ", key)

請參閱Python 演示

No match:     hello
Match found:  aaoo
Match found:  aabaa
No match:     aaaba
Match found:  bookkeeping
Match found:  bookkeeooping

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM