**Jacke:** Some text... is here?
**Hipster:** Some text... is here?
**Hipster** (ruft): Some text... is here?
**Aluhut** (brüllt hysterisch): Some text... is here?
我嘗試使用正則運算式只獲取文本,而不是第一個單詞......所以只有這個
Some text... is here?
Some text... is here?
Some text... is here?
Some text... is here?
那可能嗎?這是我的正則運算式直到現在..
([*]{2}[a-zA-Z?ü?:] [*]{2}[:]*.)([^\n]*)
更新:有沒有辦法過濾掉這樣的東西
*test* Hello it's me
(test) Hello it's me
(*test*) Hello it's me *test*
*(test)* Hello it's me **test**
所有這些的結果是:
Hello it's me
Hello it's me
Hello it's me
Hello it's me
``
uj5u.com熱心網友回復:
您可以選擇匹配括號之間的尾隨部分,并省略第一個捕獲組以匹配整個第一部分。
捕獲捕獲組中的后續部分。
[*]{2}[a-zA-Z?ü?:] [*]{2}:*(?:\s*\([^()]*\):)?\s (\S.*)
模式匹配:
[*]{2}[a-zA-Z?ü?:]匹配**并出現 1 次出現在字符類中的任何字符[*]{2}:*匹配**和可選:(?:\s*\([^()]*\):)?可選匹配空格字符后跟括號中的一部分和:\s匹配 1 個空白字符(\S.*)捕獲組 1,匹配一個非空白字符和該行的其余部分
正則運算式演示
使用 re.findall 給出捕獲組值的示例代碼:
import re
regex = r"[*]{2}[a-zA-Z?ü?:] [*]{2}:*(?:\s*\([^()]*\):)?\s (\S.*)"
s = ("**Jacke:** Some text... is here?\n"
"**Hipster:** Some text... is here?\n"
"**Hipster** (ruft): Some text... is here?\n"
"**Aluhut** (brüllt hysterisch): Some text... is here?")
print(re.findall(regex, s))
輸出
[
'Some text... is here?',
'Some text... is here?',
'Some text... is here?',
'Some text... is here?'
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358685.html
