Ignore Until "Spacebar+I or V or X" - Regex Expression

Viewed 29

So... I had a regex which worked just fine (wasn't pretty but worked), until the Roman Numerals reached more than X.
Currently my Regex looks like this:

(.*?)(^(X{1,3})(I[XV]|V?I{0,3})$|^(I[XV]|V?I{1,3})$|^V$)*(.)( EP\. )(\d*)(.*)

The problem I have right now is that if roman numeral has value 10 or more it's is in 1st group which drives me nuts.
I need it to work in a way that all before roman numerals is ignored.

Test Text:
PEPA THE PIG XVI EP. 169 - BAD ENDING

Could you please help me fix the regex so it would actually do what it suppose to do?

1 Answers

You should re-consider using anchors in the middle of a regex: ^ requires start of string and $ requires the end of string.

Besides, (.) before ( Ep\. ) consume the space, and the Ep pattern cannot match it.

Consider using

^(.*?)\b(X{1,3}(?:I[XV]|V?I{0,3})|I[XV]|V?I{1,3}|V)\b(.)\b(EP\.)\s*(\d+)(.*)

See the regex demo. You might still need to check what exactly you want to match with (.).

Details:

  • ^ - start of string
  • (.*?) - Group 1: any zero or more chars other than line break chars, as few as possible
  • \b - a word boundary
  • (X{1,3}(?:I[XV]|V?I{0,3})|I[XV]|V?I{1,3}|V) - Group 2: one to three Xs followed with IX or IV, or with an optional V and then zero to three Is, or IX, IV, or an optional V followed with one to three Is or V
  • \b - a word boundary
  • (.) - Group 3: any one char (other than a newline)
  • \b - a word boundary
  • (EP\.) - Group 4: EP.
  • \s* - zero or more whitespaces
  • (\d+) - Group 5: one or more digits
  • (.*) - Group 6: any zero or more chars other than line break chars, as many as possible
Related