0

I want to match a string for which the string elements should contain specific characters only:

  • First character from [A,C,K,M,F]
  • Followed by a number (float or integer). Allowed instances: 1,2.5,3.6,9,0,6.3 etc.
  • Ending at either of these roman numerals [I, II, III, IV, V].

The regex that I am supplying is the following

bool(re.match(r'(A|C|K|M|F){1}\d+\.?\d?(I|II|III|IV|V)$', test_str))

"(I|II|III|IV|V)" part will return true for test_str='C5.3IV' but I want to make it true even if two of the roman numerals are present at the same time with a delimiter / i.e. the regex query should retrun true for test_str='C5.3IV/V' also.

How should I modify the regex?

Thanks

kaushal
  • 55
  • 1
  • 8

2 Answers2

0

Group the delimiter and the roman numeral and treat it the same way you treat the decimal point in the float / int (you don't know whether or not it will appear but it will only appear once at most). Hope this helps!

A Coder
  • 25
  • 8
0

Try this:

bool(re.match(r'[ACKMF]\d+\.?\d?(I|II|III|IV|V)(/(I|II|III|IV|V))*$', test_str))

I also changed the start of your expression from (A|C|K|M|F){1} to [ACKMF] Characters between square brackets form a character class. Such a class matches one character out of a range of options. You most commonly see them with ranges like [A-Z0-9] to match capital letters or digits, but you can also add individual characters, as I've done for your regex.

bigblind
  • 12,539
  • 14
  • 68
  • 123