Home > OS >  How to validate using regex in python for case below
How to validate using regex in python for case below

Time:08-24

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>
  • Related