Forums

Odd Regex Behavior

I'm seeing some odd behavior with regex.

When I run my flask_app.py from the editor, code similar to this executes:

string = '1|1|بسم الله الرحمن الرحيم' 
re.match("\d{1,3}.", string).group(0)[0:-1]

But it says NoneType has no attribute "group". It's not finding a match.

Loading the same string into the Python interpreter and running the same regex match yields a result. Anyone ever experience something like this?

Hi there, are you sure you're running the same Python version in your app as you are in the interpreter?

Hi,

I realized that there was some kind of trailing space at the beginning of one of the input strings. I never noticed it, but when I used re.search on various strings with the same format and same Regex pattern, I noticed that it was returning a search object whose span was (0, 2) on all the non-error lines, and (1, 3) on the error line.

Hopefully this will help anyone doing text analysis in the future. Sometimes the input text has minute oddities that can throw off your logic. Maybe the strip() function would have been useful here. I was only using rstrip() (right strip), leaving me open to potential left trailing whitespace issues.

Hi,

I experienced the same problem with regex. It found the right result on VS Code but didnt on Pythonanywhere.

For me the fix was to add str() at the beginning of the search. Didnt work : re.search(y + "(.)" + z, paragraph.text.lower()) Did work : re.search(y + "(.)" + z, str(paragraph.text.lower()))

Hope this helps !

Hi, @flasklaw -- that's interesting. Which version of Python are you using?