Skip to content
Advertisement

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:

JavaScript

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

I have a regex like this:

JavaScript

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

JavaScript

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

Advertisement

Answer

You can use

JavaScript

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:

JavaScript

Output:

JavaScript
Advertisement