I have a string as "test string {{#}}, {{#}} , #<any text>"
I need to replace only # which comes before text(third one) and not the one in curly braces ?
Expected output should be
"test string {{#}}, {{#}} , <any text>"
here the third # is getting replaced by "" (empty string)
Iam new at regex any help is appreciated.
CodePudding user response:
s = "test string {{#}}, {{#}} , #<any text>"
# with replace
print(s.replace('#<', '<'))
test string {{#}}, {{#}} , <any text>
# with regex, where (?=<) is a look ahead
print(re.sub(r'#(?=<)','',s))
test string {{#}}, {{#}} , <any text>