I am trying to replace a selected text with a single word from that selected text using regex. I tried re.sub() but it seems that it takes the second argument "The word that I want to replace it with the text" as a string, not as regex.
Here is my string:
I go to Bridgebrook i go out <ERR targ=sometimes> some times </ERR> on Tuesday night i go to Youth <ERR targ=club> clob </ERR> . And here is my code:
# The regex of the form <ERR targ=...> .. </ERR> select_text_regex = r"<ERR[^<]+<\/ERR>" # The regex of the correct word that will replace the selected text of teh form <ERR targ=...> .. </ERR> correct_word_regex = r"targ=([^>]+)>" line = re.sub(select_text_regex, correct_word_regex, line.rstrip()) I get:
I go to Bridgebrook i go out targ=([^>]+)> on Tuesday night i go to Youth targ=([^>]+)> . My goal is:
I go to Bridgebrook i go out sometimes on Tuesday night i go to Youth club . Does Python support replacing two strings using Regex?