Recker Recker - 4 months ago 10
Python Question

Python print both the matching groups in regex

I want to find two fixed patterns from a log file. Here is a line in a log file looks like


passed dangerb.xavier64.423181.k000.drmanhattan_resources.log Aug 23
04:19:37 84526 362


From this log, I want to extract
drmanhattan
and
362
which is a number just before the line ends.

Here is what I have tried so far.

import sys
import re

with open("Xavier.txt") as f:
for line in f:
match1 = re.search(r'((\w+_\w+)|(\d+$))',line)
if match1:
print match1.groups()


However, everytime I run this script, I always get
drmanhattan
as output and not
drmanhattan 362
.

Is it because of
|
sign?

How do I tell regex to catch
this group and that group
?

I have already consulted this and this links however, it did not solve my problem.

Answer
line = 'Passed dangerb.xavier64.423181.r000.drmanhattan_resources.log Aug 23 04:19:37 84526 362'

match1 = re.search(r'(\w+_\w+).*?(\d+$)', line)
if match1:
    print match1.groups()
    # ('drmanhattan_resources', '362')

If you have a test.txt file that contains the following lines:

Passed dangerb.xavier64.423181.r000.drmanhattan_resources.log Aug 23 04:19:37 84526 362 Passed dangerb.xavier64.423181.r000.drmanhattan_resources.log Aug 23 04:19:37 84526 363 Passed dangerb.xavier64.423181.r000.drmanhattan_resources.log Aug 23 04:19:37 84526 361

you can do:

with open('test.txt', 'r') as fil:
    for line in fil:
        match1 = re.search(r'(\w+_\w+).*?(\d+)\s*$', line)
        if match1:
            print match1.groups()
# ('drmanhattan_resources', '362')
# ('drmanhattan_resources', '363')
# ('drmanhattan_resources', '361')