Is it possible to use multiple search criteria in one python regex query to return a tuple?

I have a log file that adds new logs as the program runs on different days. Each iteration will have a new product version and launch. I need Product Version: [0-9-]*

and Launch Switch: \w*

from each iteration as a tuple.

I am currently doing this:

ver = re.findall(r'(?<=Product Version: )[0-9.]*', s)

launch = re.findall(r'(?<=Launch Switch: )\w*', s)

      

Then later I iterate through ver

and launch

to create tuples. It works, but it's not very good, and I'm sure there is a more Pythonic way to do it.

+3


source to share


3 answers


You can use multiple capture groups in your regex pattern; re.findall

then will return them as a tuple. For example:

>>> data = "Product Version: 0.0.1 | Launch Switch: hello | Product Version: 2.3.4 | Launch Switch: world"
>>> re.findall("Product Version: ([0-9.]+).*?Launch Switch: (\w+)", data)
[('0.0.1', 'hello'), ('2.3.4', 'world')]

      



From the re.findall

docs
:

Returns all matching pattern matches in a string, as a list of strings. The string is scanned from left to right, and matches are returned in the order found. If one or more groups are present in the template, return a list of groups; it will be a list of tuples if the template has more than one group . Blank matches are included in the result unless they are related to the start of another match.

+3


source


You can use zip

:

ver_launch_tuples = zip(re.findall(r'(?<=Product Version: )[0-9.]*', s),
                        re.findall(r'(?<=Launch Switch: )\w*', s))

      



This will create a list of (ver, launch) tuples.

+2


source


I'm not sure if this is "pythonic", but it will get you your tuple from a string that has these elements in any order:

import re
str = """Product Version: 23423
Launch Switch: foiwjefj"""
tuple([word for sublist in [[x for x in f if x != ''] for f in re.findall(
    r'Product Version: (\d+)|Launch Switch: (\w+)', str)] for word in sublist])

      

This, of course, makes some assumptions:

  • Product Version

    and are Launch Swtich

    always included in this order
  • Not empty
  • There is only one of them in the line.
0


source







All Articles