我沒有太多使用正則運算式,并且在嘗試在我需要決議的一長串文本中拆分 3 條特定資訊時遇到問題。
note = "**Jane Greiz** `#1`: Should be open here .\n**Thomas Fitzpatrick** `#90`: Anim: Can we start the movement.\n**Anthony Smith** `#91`: Her left shoulder.\nhttps://google.com"
- pattern1 = 決議**名稱文本**
- pattern2 = 決議數字`#x`
- 模式 3 = 抓住其他所有東西,直到下一個模式 1
我所擁有的似乎效果不佳。有空元素嗎?他們不是分組在一起的嗎?而且我不知道如何在不影響前兩個模式的情況下獲取最后一個模式文本。如果所有 3 個匹配項都在一個元組中而不是分開的,我也會喜歡它。這是我到目前為止所擁有的:
all = r"\*\*(. ?)\*\*|\`#(. ?)\`:"
l = re.findall(all, note)
輸出:
[('Jane Greiz', ''), ('', '1'), ('Thomas Fitzpatrick', ''), ('', '90'), ('Anthony Smith', ''), ('', '91')]
uj5u.com熱心網友回復:
不要使用替代品。將名稱和數字模式一個接一個地放在一個選項中,然后為匹配添加另一個組直到下一個**
。
note = "**Jane Greiz** `#1`: Should be open here .\n**Thomas Fitzpatrick** `#90`: Anim: Can we start the movement.\n**Anthony Smith** `#91`: Her left shoulder.\nhttps://google.com"
all = r"\*\*(. ?)\*\*.*?\`#(. ?)\`:(.*)"
print(re.findall(all, note))
輸出是:
[('Jane Greiz', '1', ' Should be open here .'), ('Thomas Fitzpatrick', '90', ' Anim: Can we start the movement.'), ('Anthony Smith', '91', ' Her left shoulder.')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/531697.html
標籤:Python正则表达式