Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex to find a string or another string in search and then return one part of the searched with group

I have a string which is a file name, examples:

'20220213-0000-FSC-814-SC_VIRG_REFBAL_PRES_NPMINMAX-v1.xml'
'20220213-0000-F814-SC_VIRG_REFBAL_PRES_NPMINMAX-v1.xml'

I want to find a string with re.search which corresponds to Fddd or FSC-ddd.

I have a regex like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

type_match = re.search(r'(F(\d{3}))|(FSC-(\d{3}))', string)

Later after I have found for example FSC-814 , I want to get only the number from this found string, I used:

int(type_match.group(1))

but it does not work after I included or statement in the re.search

>Solution :

You can use

F(?:SC)?-?(\d{3})

See the regex demo.

Details:

  • F – an F char
  • (?:SC)? – an optional SC char sequence
  • -? – an optional hyphen
  • (\d{3}) – Capturing group 1: three digits.

See the Python demo:

import re
texts = ['20220213-0000-FSC-814-SC_VIRG_REFBAL_PRES_NPMINMAX-v1.xml',
'20220213-0000-F814-SC_VIRG_REFBAL_PRES_NPMINMAX-v1.xml']
pattern = r'F(?:SC)?-?(\d{3})'
for text in texts:
    match = re.search(pattern, text)
    if match:
        print (match.group(1))

Output:

814
814
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading