Why do Python regex spans extend one place past the actual match? -



Why do Python regex spans extend one place past the actual match? -

looking @ spans returned regex matches, noticed homecoming 1 past actual match; e.g. in illustration @ regular look howto

>>> print(p.match('::: message')) none >>> m = p.search('::: message'); print(m) <_sre.sre_match object @ 0x...> >>> m.group() 'message' >>> m.span() (4, 11)

the resulting span in illustration (4, 11) vs. actual location (4, 10). causes problem me left-hand , right-hand boundaries have different meanings , need compare relative positions of spans.

is there reason or can go ahead , modify spans liking subtracting 1 right boundary?

because in python, slicing , ranges never end value exclusive, , '::: message'[4:11] reflects actual matched text:

>>> '::: message'[4:11] 'message'

thus, can utilize matchobject.span() results piece matched text original string:

>>> import re >>> s = '::: message' >>> match = p.search(s) >>> match.span() (4, 11) >>> s[slice(*match.span())] 'message'

python regex

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -