2

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)
1
  • 1
    Use re.findall(r'\bMatch-([^)]+)', inp, flags=re.I) Commented Jan 2, 2021 at 7:53

3 Answers 3

2

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.