Home > Blockchain >  How to replace a specific character after the occurrence of a string using regex
How to replace a specific character after the occurrence of a string using regex

Time:07-26

I have a specific requirement- I need to replace all '@' symbols after a '~' is encountered in a string using regular expression in python.

Sample inputs-

a) //div[@id='1234'] ~ div[@id='abcd'] ~ a[@href='/xyz']

b) //div[@id='1234']/div[@id='abcd'] ~ a[@href='/xyz']

Desired output-

a) //div[@id='1234'] ~ div[id='abcd'] ~ a[href='/xyz']

b) //div[@id='1234']/div[@id='abcd'] ~ a[@href='/xyz']

Note- The position of the '~' symbol can appear anywhere and we need to replace only after it has been encountered.

I have tried to create a few regex but not been successfull so far. Any help?

CodePudding user response:

Use regex pattern with two capture groups:

r"(~[^@]*)(@)"   # first capture group starts with ~ and excludes @
                 # 2nd capture group just has @

Substitution keeps first capture group in string s

re.sub(r"(~[^@]*)(@)", r"\1", s)

Example:

print(re.sub(r"(~[^@]*)(@)", r"\1", r"//div[@id='1234'] ~ div[@id='abcd'] ~ a[@href='/xyz']")
# Output: "//div[@id='1234'] ~ div[id='abcd'] ~ a[href='/xyz']"

CodePudding user response:

You can do it without regexes too - find the index of ~ in your string and replace @ characters after that index

s = "//div[@id='1234'] ~ div[@id='abcd'] ~ a[@href='/xyz']"
out_s = s[:s.index('~')]   s[s.index('~'):].replace('@', '')

Output

"//div[@id='1234'] ~ div[id='abcd'] ~ a[href='/xyz']"
  • Related