[英]Python regex multiple matching groups
我正在使用一個小的實用程序來查找Windows下文件的類型。
TrID/32 - File Identifier v2.10 - (C) 2003-11
By M.Pontello Definitions found: 5295 Analyzing...
Collecting data from file: april_error.wmv
94.1% (.WMV/WMA) Windows Media (generic) (16018/6)
5.8% (.CAT) Microsoft Security Catalog (1000/1)
在Python中,如何捕獲(.WMV/WMA)
導致似乎當前獲得了錯誤的匹配組。 例如re.search('\\((.*?)\\)', stdout).group(1)
返回'C'
提前致謝。
嘗試改用findall
:
a = re.findall('\((.*?)\)', stdout)
>>> print a
['C','.WMV/WMA','generic','16018/6','.CAT','1000/1']
>>> print a[1]
.WMV/WMA
或按照@tobias_k建議,執行以下操作以僅捕獲文件擴展名匹配項:
a = re.findall('\((\..*?)\)', stdout)
>>> print a
['.WMV/WMA', '.CAT']
根據上面的評論,這是您需要的:
match = re.search(r"% \([a-z.]+/[a-z.]+\)", subject, re.IGNORECASE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.