简体   繁体   中英

Python Regex - replace a string not located between two specific words

Given a string, I need to replace a substring with another in an area not located between two given words.

For example:

substring: "ate" replace to "drank", 1st word - "wolf", 2nd word - "chicken"

input:  The wolf ate the chicken and ate the rooster
output: The wolf ate the chicken and drank the rooster

Currently, the only solution I have is extremely unclean:

1) Replace the string located between the two words to a temporary substring, via Replace a string located between

2) replace the string I originally wanted

3) revert the temporary string to the original string

Edit:

I specifically asked a slightly different question than my case to keep the answer relevant for future readers.

My specific need is splitting a string according to ":", when I need to disregard ":" that are between "<" and ">" brackets that can be chained, where the only promise is that the number of opening brackets equal the number of closing brackets.

So for example, In the following case:

input  a : <<a : b> c> : <a < a < b : b> : b> : b> : a
output [a, <<a : b> c>, <a < a < b : b> : b> : b>, a]

If the answers are very different, I'll start another question.

def repl(match):
    if match.group()=="ate":
        return "drank"
    return  match.group()


x="The wolf ate the chicken and ate the rooster"
print re.sub(r"(wolf.*chicken)|\bate\b",repl,x)

You can use a function for replacement to do the trick with re.sub

Use re.sub one-liner function.

>>> s = "The wolf ate the chicken and ate the rooster"
>>> re.sub(r'wolf.*?chicken|\bate\b', lambda m: "drank" if m.group()=="ate" else m.group(), s)
'The wolf ate the chicken and drank the rooster'

Update:

Updated problem would be solved by using regex module.

>>> s = "a : <<a : b> c> : <a < a < b : b> : b> : b> : a"
>>> [i for i in regex.split(r'(<(?:(?R)|[^<>])*>)|\s*:\s*', s) if i]
['a', '<<a : b> c>', '<a < a < b : b> : b> : b>', 'a']

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM