简体   繁体   中英

Python regex multiple matching groups

I am using a small utility to find the type a file under 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)

In Python, how can I capture the (.WMV/WMA) cause it seems that I currently get a wrong matching group. For instance re.search('\\((.*?)\\)', stdout).group(1) returns 'C'

Thanks in advance.

Try using findall instead:

a = re.findall('\((.*?)\)', stdout)

>>> print a
['C','.WMV/WMA','generic','16018/6','.CAT','1000/1']

>>> print a[1]
.WMV/WMA

Or as @tobias_k suggested, do the following to only capture the file extension matches:

a = re.findall('\((\..*?)\)', stdout)

>>> print a
['.WMV/WMA', '.CAT']

根据上面的评论,这是您需要的:

match = re.search(r"% \([a-z.]+/[a-z.]+\)", subject, re.IGNORECASE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM