Replacing numbers in a string after the match.
t = 'String_BA22_FR22_BC'
re.sub(r'FR(\d{1,})','',t)
My desired output would be String_BA22_FR_BC
You may use
re.sub(r'FR\d+','FR',t)
Alternatively, you may capture the part you need to keep with a capturing group and replace with \1 backreference:
re.sub(r'(FR)\d+', r'\1', t)
^--^- >>>----^
See the Python demo
A capturing group approach is flexible as it allows patterns of unlimited length.
re.sub(r'(FR|ZZ)\d+', r'\1', t). This allowing me search multiple character groups.re.sub(r'((?:FR|ZZ)[a-z]*)\d+', r'\1', t) using this approach if need be.[a-z] is checking for upper or lower case?[A-Za-z]* will match 0 or more ASCII letters. [^\W\d_]* will match any 0+ letters. [a-z]* only matches 0+ lowercase ASCII letters if re.I flag is not specified.You are replacing what you match (in this case FR22) with an empty string.
Another option is to use a positive lookbehind an then match 1+ digits adn replace that match with an empty string:
(?<=FR)\d+
For example:
re.sub(r'(?<=FR)\d+','',t)
(?<=FR)\d+