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

2024/10/3 4:33:29

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.

Answer
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

https://en.xdnf.cn/q/70771.html

Related Q&A

Vectorized Lookups of Pandas Series to a Dictionary

Problem Statement:A pandas dataframe column series, same_group needs to be created from booleans according to the values of two existing columns, row and col. The row needs to show True if both cells …

Why cant I get my static dir to work with django 1.3?

This problem is very simple, but I just cant figure it outadded to my urlpatternsurl(r^static/(?P<path>.*)$, django.views.static.serve, {document_root: /home/user/www/site/static})where my main.…

Desktop Launcher for Python Script Starts Program in Wrong Path

I can not launch a python script from a .desktop launcher created on Linux Mint 17.1 Cinnamon.The problem is that the script will be launched in the wrong path - namely the home folder instead of the d…

How does numpy broadcasting perform faster?

In the following question, https://stackoverflow.com/a/40056135/5714445Numpys broadcasting provides a solution thats almost 6x faster than using np.setdiff1d() paired with np.view(). How does it manage…

python check if utf-8 string is uppercase

I am having trouble with .isupper() when I have a utf-8 encoded string. I have a lot of text files I am converting to xml. While the text is very variable the format is static. words in all caps should…

Failed building wheel for Twisted in Windows 10 python 3

Im trying to install rasa-core on my windows 10 machine.When installing with pip install, I get: Failed building wheel for TwistedThe same error appears when trying to install Twisted separately.How co…

How to set a fill color between two vertical lines

Using matplotlib, we can "trivially" fill the area between two vertical lines using fill_between() as in the example: https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/fill_between_…

Pythonic way to read file line by line?

Whats the Pythonic way to go about reading files line by line of the two methods below?with open(file, r) as f:for line in f:print lineorwith open(file, r) as f:for line in f.readlines():print lineOr …

Python Selenium clicking next button until the end

This is a follow up question from my first question, and I am trying to scrape a website and have Selenium click on next (until it cant be clicked) and collect the results as well.This is the html tag …

How can i detect one word with speech recognition in Python

I know how to detect speech with Python but this question is more specific: How can I make Python listening for only one word and then returns True if Python could recognize the word.I know, I could ju…