Python's 'match' Line In _sre.sre_match Output? Can It Show The Full Match?
Using python 3: In [275]: blah = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg' In [276]: pat = re
Solution 1:
No, it is not possible. That length is hard coded in the format string in the match object's repr
method and is not designed to capture the full length of the matched string.
Except you compile your own build of CPython (or whatever Python flavour you're at) with the precision of the representation of the match
object in match_repr
modified. Default precision is 50:
result = PyUnicode_FromFormat(
"<%s object; span=(%d, %d), match=%.50R>",
Py_TYPE(self)->tp_name,
self->mark[0], self->mark[1], group0);
As others have suggested, simply use the group
method of the match object to access the full matching string(s).
Solution 2:
With this one you don't constrain yourself with any upper limit of repetitions:
import re
blah = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg"
r = re.compile(r'[a-z](.)\1{2,}') # no upper limittry:
re.search(r, blah).group()
except AttributeError:
pass# whatever you want to do when there is no match
Solution 3:
You are probably looking for the .group ...
import re
blah = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg"
pat = re.compile("([a-z]{2,99})")
data = re.search(pat,blah)
ifdata:
data.group(0) # returns 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'else:
pass # do something when the string was not found!
See: https://docs.python.org/3.5/library/re.html#re.match.group
and
Why won't re.groups() give me anything for my one correctly-matched group?
Post a Comment for "Python's 'match' Line In _sre.sre_match Output? Can It Show The Full Match?"