简体   繁体   中英

Recursively matching each non-digit characters when preceded by a digit

I'm trying to match each individual non-digit characters that are directly preceded by a digit. The idea is to replace O with 0 in numbers.

Eg:

58OO
1O1O
2OOOm
FOO
O

The desired output is

5800
1010
2000m
FOO
O

I tried using the answer in a previous question I asked but I don't manage to adapt the regex to my purpose.

Here are a few regex I tried, without success (and for good reasons): (\d\K(?>O|(?1))) or \d\K(?:O|(?R)) .

Nothing to do with recursion, just use a simple while loop:

import re

strings = ["58OO", "1O1O", "2OOOm", "FOO", "O"]

pattern = re.compile(r'(?<=\d)O')

for item in strings:
    while True:
        olditem = item
        item = pattern.sub("0", item)
        if item == olditem:
            # no replacement was made
            break

    print(item)

This yields

5800
1010
2000m
FOO
O

Time to install PyPi regex module :

import regex
texts = ["58OO", "1O1O", "2OOOm", "FOO", "O"]
for text in texts:
  print( regex.sub(r'(?<=\d+O*)O', '0', text) )

Output:

5800
1010
2000m
FOO
O

The (?<=\d+O*)O expression matches O that has digit(s) and any amount of O s before.

A simple sub is more than enough.

Example from the python interpreter:

>>> import re
>>> raw = '''
58OO
O1O1O
2OOOm
FOO
O
'''
>>>
>>> print(re.sub(r'(\d+)(O+)', lambda m: m.group(1) +  '0' * len(m.group(2)), raw))

5800
O1010
2000m
FOO
O

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM