Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is SRE_Match

Tags:

python

regex

I need to check if a variable is a regular expression match object.

print(type(m)) returns something like that: <_sre.SRE_Match object at 0x000000000345BE68>

but when I import _sre and try to execute type(m) is SRE_Match the exception NameError: name 'SRE_Match' is not defined is raised.

like image 630
Teddy Bo Avatar asked Sep 20 '12 06:09

Teddy Bo


1 Answers

You can do

SRE_MATCH_TYPE = type(re.match("", ""))

at the start of the program, and then

type(m) is SRE_MATCH_TYPE

each time you want to make the comparison.

like image 199
David Robinson Avatar answered Sep 30 '22 21:09

David Robinson