简体   繁体   中英

Substitution of ACD|BCD like regular expression in python

A, B, C and D represent different parts of a regular expression.

The effect I want to achieve: The input string is ACD or BCD. After substituting C with E, the output should be AED or BED.

The regular expression I used:

r=(A)C(D)|(B)C(D)

However, the problem arose when I did the substitution. If I use r.sub(r'\\1s0\\2',inputstring) then there will be an unmatched-group error when the input is BCD. If I use r.sub(r'\\3s0\\4',inputstring) then there will be an unmatched-group error when the input is ACD.

So how can I edit the regular expression to avoid this situation?

Use (A|B)C(D) instead of (A)C(D)|(B)C(D) :

import re
r = re.compile(r'(A|B)C(D)')
r.sub(r'\1E\2', 'ACD')    # 'AED'
r.sub(r'\1E\2', 'BCD')    # 'BED'

You can use a substitution function instead of string. The return value of the function is used as a replacement string.

import re

def repl(m):
    # m: the matched object.
    if m.group(1) is not None:
        prefix, suffix = m.group(1), m.group(2)
    else:
        prefix, suffix = m.group(3), m.group(4)
    return '{}E{}'.format(prefix, suffix)

re.sub('(A)C(D)|(B)C(D)', repl, 'ACD') # AED
re.sub('(A)C(D)|(B)C(D)', repl, 'BCD') # BED

Alternatively, if you use regex module instead of Python builtin re module, you can do following:

>>> import regex # NOTE: not `re`, but `regex`
>>>
>>> regex.sub('(A)C(D)|(B)C(D)', r'\1\3E\2\4', 'ACD')
'AED'
>>> regex.sub('(A)C(D)|(B)C(D)', r'\1\3E\2\4', 'BCD')
'BED'

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