Regex with lookbehind not working using re.match

2024/11/17 23:57:32

The following python code:

import reline="http://google.com"
procLine = re.match(r'(?<=http).*', line)
if procLine.group() == "":print(line + ": did not match regex")
else:print(procLine.group())

does not match successfully, and outputs the following error:

Traceback (most recent call last): File"C:/Users/myUser/Documents/myScript.py", line 5,in if procLine.group() == "": AttributeError: 'NoneType' object has no attribute 'group'

When I replace the regex with just .* it works fine which suggests it's the regex that is in error, however, on https://regex101.com/ when I test my regex and string for python flavor it appears to match fine.

Any ideas?

Answer

If you convert your lookbehind to a non-capturing group, this should work:

In [7]: re.match(r'(?:http://)(.*)', line)
Out[7]: <_sre.SRE_Match object; span=(0, 17), match='http://google.com'>In [8]: _.group(1)
Out[8]: 'google.com'

The reason a lookbeind does not work is because - as Rawing mentioned - re.match starts looking from the start of the string, so a lookbehind at the start of a string does not make sense.


If you insist on using a lookbehind, switch to re.search:

In [10]: re.search(r'(?<=http://).*', line)
Out[10]: <_sre.SRE_Match object; span=(7, 17), match='google.com'>In [11]: _.group()
Out[11]: 'google.com'
https://en.xdnf.cn/q/71129.html

Related Q&A

testing python multiprocessing pool code with nose

I am trying to write tests with nose that get set up with something calculated using multiprocessing.I have this directory structure:code/tests/tests.pytests.py looks like this:import multiprocessing a…

Python verify url goes to a page

I have a list of urls (1000+) which have been stored for over a year now. I want to run through and verify them all to see if they still exist. What is the best / quickest way to check them all and re…

Bokeh: Synchronizing hover tooltips in linked plots

I have two linked plots. When hovering, I would like to have a tooltip appear in both plots. I already use the linked selection with great success, but now I want to link the tooltips also.Below is an …

Pipe STDIN to a script that is itself being piped to the Python interpreter?

I need to implement an SVN pre-commit hook which executes a script that itself is stored in SVN.I can use the svn cat command to pipe that script to the Python interpreter, as follows:svn cat file://$R…

subprocess.call using cygwin instead of cmd on Windows

Im programming on Windows 7 and in one of my Python projects I need to call bedtools, which only works with Cygwin on Windows. Im new to Cygwin, installed the default version + everything needed for be…

Django Celery Received unregistered task of type appname.tasks.add

Following the documentation and the Demo Django project here https://github.com/celery/celery/tree/3.1/examples/djangoProject Structurepiesup2|piesup2| |__init__.py| |celery.py| |settings.py| |urls…

Documenting and detailing a single script based on the comments inside

I am going to write a set of scripts, each independent from the others but with some similarities. The structure will most likely be the same for all the scripts and probably looks like: # -*- coding: …

Using Ansible variables in testinfra

Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running teststest.pyimport pytest def test_zabbix_agent_package(host):package = host.pa…

How to create a dictionary of dictionaries of dictionaries in Python

So I am taking a natural language processing class and I need to create a trigram language model to generate random text that looks "realistic" to a certain degree based off of some sample da…

How to separate Master Slave (DB read / writes) in Flask Sqlalchemy

Im trying to separate the Read and write DB operations via Flask Sqlalchemy. Im using binds to connect to the mysql databases. I would want to perform the write operation in Master and Reads from slave…