Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

regex

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.

like image 599
ErezO Avatar asked Apr 16 '15 12:04

ErezO


1 Answers

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

like image 185
vks Avatar answered Sep 28 '22 16:09

vks