Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex. Match and replace roman numerals

Need some help with regular expressions. I want to match some Roman numerals and replace them to arabic.

First of all if use (IX|IV|V?I{0,3}) to match roman numerals (from 1 to 9). Then i add some logic to either space (with some text before) or nothing (begin/end of string) with (?:^|\s)(?:\s|$)

So finaly i've (?:^|\s)(IX|IV|V?I{0,3})(?:\s|$)

It matches all this variants:

  1. some text VI
  2. IX here we are
  3. another III text

If i define dict with roman-arabic map {'iii': 3, 'IX': 9} - how to repalce matches with values from dict? Also it matches only first accur, i.e. in some V then III i get only V

like image 338
Digital God Avatar asked Jun 12 '26 19:06

Digital God


1 Answers

Also it matches only first accur, i.e. in some V then III i get only V

I assume that you are using re.match or re.search which is only giving you one result. We will use re.sub to solve your main question so this won't be an issue. re.sub can take a callable. We replace any match with the corresponding value from your dictionary. Use

re.sub(your_regex, lambda m: your_dict[m.group(1)], your_string)

This assumes any possible match is in your dict. If not, use

re.sub(your_regex, lambda m: your_dict[m.group(1)] if m.group(1) in your_dict else m.group(1), your_string)
like image 178
timgeb Avatar answered Jun 14 '26 07:06

timgeb