stratis
stratis

Reputation: 8050

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.

Upvotes: 0

Views: 60

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 99081

Based on your comment above, this is what you need:

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

Upvotes: 1

sshashank124
sshashank124

Reputation: 32207

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']

Upvotes: 2

Related Questions