我有這些字串:
[[:File:Example.jpg]]
[[:File:Example.jpg|this example]]
[[Media:Example.jpg]]
[[Georgia (U.S. state)|Georgia]]
[[Arkansas]]
[[Canada]]
[[Virginia]]
[[Image:Houstonia longifolia - Long Leaf Bluet 2.jpg|thumb|left]]
我想提取re與開始字串[[Image或[[Media:或[[:file:
uj5u.com熱心網友回復:
要查找以 開頭的字串[[:File:,您可以使用:
re.search(r"\[\[:File.*?]]", your_strings)
與[[Media:和相同[[Image:
re.search(r"\[\[Media:.*?]]", your_strings)
re.search(r"\[\[Image.*?]]", your_strings)
請參閱此示例。
uj5u.com熱心網友回復:
試試這個正則運算式
僅當存在[[Image,[[Media:或[[:File:在字串開頭時才輸出(還添加了re.IGNORECASE標志以在任何情況下進行匹配)
\[\[(?:Image|Media|:File):. ]]
代碼:
import re
a = '''[[:File:Example.jpg]]
[[:File:Example.jpg|this example]]
[[Media:Example.jpg]]
[[Georgia (U.S. state)|Georgia]]
[[Arkansas]]
[[Canada]]
[[Virginia]]
[[Image:Houstonia longifolia - Long Leaf Bluet 2.jpg|thumb|left]]'''
print(re.findall(r'\[\[(?:Image|Media|:File):. ]]', a, flags=re.IGNORECASE))
輸出:
[
'[[:File:Example.jpg]]',
'[[:File:Example.jpg|this example]]',
'[[Media:Example.jpg]]',
'[[Image:Houstonia longifolia - Long Leaf Bluet 2.jpg|thumb|left]]'
]
Regex101 演示
告訴我它是否不起作用...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/342415.html
