Python regex not to match http://

2024/9/20 8:07:13

I am facing a problem to match and replace certain words, not contained in http://

Present Regex:

 http://.*?\s+

This matches the pattern http://www.egg1.com http://www.egg2.com

I need a regex to match certain words contained outside the http://

Example:

"This is a sample. http://www.egg1.com and http://egg2.com. This regex will only match this egg1 and egg2 and not the others contained inside http:// "Match: egg1 egg2Replaced: replaced1 replaced2

Final Output :

 "This is a sample. http://www.egg1.com and http://egg2.com. This regex will only match this replaced1 and replaced2 and not the others contained inside http:// "

QUESTION: Need to match certain patterns (as in example : egg1 egg2) unless they are part of http:// .Do not match egg1 and egg2 if they are present within http://

Answer

One solution I can think of is to form a combined pattern for HTTP-URLs and your pattern, then filter the matches accordingly:

import ret = "http://www.egg1.com http://egg2.com egg3 egg4"p = re.compile('(http://\S+)|(egg\d)')
for url, egg in p.findall(t):if egg:print egg

prints:

egg3
egg4

UPDATE: To use this idiom with re.sub(), just supply a filter function:

p = re.compile(r'(http://\S+)|(egg(\d+))')def repl(match):if match.group(2):return 'spam{0}'.format(match.group(3))return match.group(0)print p.sub(repl, t)

prints:

http://www.egg1.com http://egg2.com spam3 spam4
https://en.xdnf.cn/q/72521.html

Related Q&A

how can I maintain sequence of my list using set?

In [1]: l1 = [a,2,3,0,9.0,0,2,6,b,a]In [2]: l2 = list(set(l1))In [3]: l2 Out[3]: [a, 0, 2, 3, 6, 9.0, b]Here you can see the the list l2 is falling with different sequence then the original l1, I need …

How to reference a dict object?

I have a Python dict object d. d = {a: 1, b: 2, c: 3}. My problem is really simple. I want to reference a variable to the elements of d. For example, something like:In[1]: p = d[a] >>> p = 1 I…

Python 3.3: DeprecationWarning when using nose.tools.assert_equals

I am using nosetest tools for asserting a python unittest:... from nose.tools import assert_equals, assert_almost_equalclass TestPolycircles(unittest.TestCase):def setUp(self):self.latitude = 32.074322…

Panel/Hvplot interaction when variable is changing

Im trying to create a dashboard with two holoviews objects: a panel pn.widgets.Select object that contains a list of xarray variables, and a hvplot object that takes the selected variable on input, lik…

asyncio matplotlib show() still freezes program

I wish to run a simulation while at the same time output its progress in a plot. Ive been looking through a lot of examples of threading and multiprocessing, but they are all pretty complex. So I thoug…

celery .delay hangs (recent, not an auth problem)

I am running Celery 2.2.4/djCelery 2.2.4, using RabbitMQ 2.1.1 as a backend. I recently brought online two new celery servers -- I had been running 2 workers across two machines with a total of ~18 thr…

Python ttk.combobox force post/open

I am trying to extend the ttk combobox class to allow autosuggestion. the code I have far works well, but I would like to get it to show the dropdown once some text has been entered without removing fo…

How to pull from the remote using dulwich?

How to do something like git pull in python dulwich library.

convert python programme to windows executable

i m trying to create windows executable from python program which has GUI . i m using following scriptfrom distutils.core import setup import py2exesetup(console=[gui.py]) it gives following errorWarni…

Must explicitly set engine if not passing in buffer or path for io in Panda

When running the following Python Panda code:xl = pd.ExcelFile(dataFileUrl)sheets = xl.sheet_namesdata = xl.parse(sheets[0])colheaders = list(data)I receive the ValueError:Must ex…