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:
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With