Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect re (regexp) object in Python

Tags:

I wonder what is the proper pythonic backward- and forward-compatible method how check if an object is compiled re object.

isinstance method cannot be easily used, while the resulting object claims to be _sre.SRE_Pattern object:

>>> import re
>>> rex = re.compile('')
>>> rex
<_sre.SRE_Pattern object at 0x7f63db414390>

but there is no such one:

>>> import _sre
>>> _sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

>>> import sre
__main__:1: DeprecationWarning: The sre module is deprecated, please import re.
>>> sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

>>> re.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

I don't want to use duck typing (i.e. checking for the availability of some specific methods), because this could collide with some other types.

For now, I'm using:

>>> RegexpType = type(re.compile(''))
>>> type(rex) == RegexpType
True

but there might be a better way..

like image 643
mykhal Avatar asked Jun 03 '11 10:06

mykhal


People also ask

How do I use the re lookup function in Python?

Python regex re.search() method looks for occurrences of the regex pattern inside the entire target string and returns the corresponding Match Object instance where the match found. The re.search() returns only the first match to the pattern from the target string.

What is re match in Python?

Both return the first match of a substring found in the string, but re. match() searches only from the beginning of the string and return match object if found. But if a match of substring is found somewhere in the middle of the string, it returns none.

What is re in regex?

A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

How do I find a string in a Python pattern?

The basic rules of regular expression search for a pattern within a string are: The search proceeds through the string from start to end, stopping at the first match found. All of the pattern must be matched, but not all of the string.


2 Answers

re._pattern_type exists, and appears to do what you want:

>>> isinstance(re.compile(''), re._pattern_type)
True

But this is not a good idea - per Python convention, names starting with _ are not part of the public API of a module and not part of the backward compatibility guarantees. So, using type(re.compile('')) is your best bet - though notice that this isn't guaranteed to work either, since the re module makes no mention of the object returned from re.compile() being of any particular class.

And indeed, even if this was guaranteed, the most Pythonic and back- and forward- compatible way would be to rely on the interface, rather than the type. In other words, embracing duck typing and EAFP, do something like this:

try:
     rex.match(my_string)
except AttributeError:
     # rex is not an re
else:
     # rex is an re
like image 107
lvc Avatar answered Oct 05 '22 13:10

lvc


Following some of the recommendations you can come up with:

import re

# global constant
RE_TYPE = re.compile('').__class__

def is_regex(a):
    return isinstance(a, RE_TYPE)
like image 31
Totoro Avatar answered Oct 05 '22 12:10

Totoro