Detecting a specific string with regex in python

Viewed 86

I have a csv file with below string structure:

Modem Switch (MMA-213-MML-NW-Match-New Year)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New1 Month)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New Month 2)(32655)(12532)
....

I want to get any string which comes after Match: For example the expected result shared as below:

New Year
New Month
New1 Month
New Month 2

with below code it is not possible to get my relative string:

matches = re.findall(r'(Match-)(\w+)', inp, flags=re.I)
3 Answers

This works:

import re
inp = "Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)"
matches = re.findall(r'Match-(.+?)\)', inp, flags=re.I)

gives

['New Month']

You could also match all following word characters with spaces in between, and use a single capturing group.

\bMatch-(\w+(?:[^\S\r\n]+\w+)*)

Regex demo | Python demo

import re
 
regex = r"\bMatch-(\w+(?:[^\S\r\n]+\w+)*)"
 
s = ("Modem Switch (MMA-213-MML-NW-Match-New Year)(32655)(12532)\n"
    "Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)\n"
    "Modem Switch3 (MMA-1234-431-NW-Match-New1 Month)(32655)(12532)\n"
    "Modem Switch3 (MMA-1234-431-NW-Match-New Month 2)(32655)(12532)")
 
print(re.findall(regex, s))

Output

['New Year', 'New Month', 'New1 Month', 'New Month 2']

Or to match all after Match- between parenthesis, you could use a negated character class matching any character except ( and )

\([^()]*\bMatch-([^()]+)\)

Regex demo

Use

re.findall(r'(?<=Match-)[^()]+', inp, flags=re.I)

See regex proof.

Explanation

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    Match-                   'Match-'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  [^()]+                   any character except: '(', ')' (1 or more
                           times (matching the most amount possible))

Python code:

import re
inp = """Modem Switch (MMA-213-MML-NW-Match-New Year)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New Month)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New1 Month)(32655)(12532)
Modem Switch3 (MMA-1234-431-NW-Match-New Month 2)(32655)(12532)"""
print(re.findall(r'(?<=Match-)[^()]+', inp, flags=re.I))

Results: ['New Year', 'New Month', 'New1 Month', 'New Month 2']

Related