Home > Back-end >  Python: re.sub removing words between two delimiters under different cases
Python: re.sub removing words between two delimiters under different cases

Time:12-13

I have a string that contains patterns like

"something [[remove|keep]] something [[keep]] something [[remove|remove|keep]] something" 

How can I use re.sub() or other methods to edit this string to

"something keep something keep something keep something"

or

"something [[keep]] something [[keep]] something [[keep]] something". 

(I can remove the [[ ]]s afterward.)

I tried using for-loop to hard code it, but the string is too long and takes a long time to run.

CodePudding user response:

You can use the regular expression: (?<=[\[\|])[^\[]*?\|:

import re

s = "something [[remove|keep]] something [[keep]] something [[remove|remove|keep]] something"
print(re.sub("(?<=[\[\|])[^\[]*?\|", "", s))

Output:

something [[keep]] something [[keep]] something [[keep]] something

The regular expression (?<=[\[\|])[^\[]*?\| can be broken down into three parts to return substrings that...

  • (?<=[\[\|]) - begins with either [ or |
  • [^\[]*? - contains anything (non-greedy) that is not a [
  • \| - and ends with |
  • Related