[issue42550] re库匹配问题
Karthikeyan Singaravelan
report at bugs.python.org
Thu Dec 3 01:30:40 EST 2020
Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:
https://docs.python.org/3/howto/regex.html#more-metacharacters
$
Matches at the end of a line, which is defined as either the end of the string, or any location followed by a newline character.
\Z
Matches only at the end of the string.
>>> eth_re = re.compile(r'^0x[0-9a-fA-F]{40}\Z')
>>> print(eth_re.match(a))
None
>>> eth_re = re.compile(r'^0x[0-9a-fA-F]{40}$')
>>> print(eth_re.match(a))
<re.Match object; span=(0, 42), match='0xd26935a5ee4cd542e8a3a7e74fb7a99855975b59'>
You can also use re.DEBUG to see the difference
>>> re.match(r'^0x[0-9a-fA-F]{40}$', a, re.DEBUG)
AT AT_BEGINNING
LITERAL 48
LITERAL 120
MAX_REPEAT 40 40
IN
RANGE (48, 57)
RANGE (97, 102)
RANGE (65, 70)
AT AT_END
0. INFO 4 0b0 42 42 (to 5)
5: AT BEGINNING
7. LITERAL 0x30 ('0')
9. LITERAL 0x78 ('x')
11. REPEAT_ONE 16 40 40 (to 28)
15. IN 11 (to 27)
17. CHARSET [0x00000000, 0x03ff0000, 0x0000007e, 0x0000007e, 0x00000000, 0x00000000, 0x00000000, 0x00000000]
26. FAILURE
27: SUCCESS
28: AT END
30. SUCCESS
<re.Match object; span=(0, 42), match='0xd26935a5ee4cd542e8a3a7e74fb7a99855975b59'>
>>> re.match(r'^0x[0-9a-fA-F]{40}\Z', a, re.DEBUG)
AT AT_BEGINNING
LITERAL 48
LITERAL 120
MAX_REPEAT 40 40
IN
RANGE (48, 57)
RANGE (97, 102)
RANGE (65, 70)
AT AT_END_STRING
0. INFO 4 0b0 42 42 (to 5)
5: AT BEGINNING
7. LITERAL 0x30 ('0')
9. LITERAL 0x78 ('x')
11. REPEAT_ONE 16 40 40 (to 28)
15. IN 11 (to 27)
17. CHARSET [0x00000000, 0x03ff0000, 0x0000007e, 0x0000007e, 0x00000000, 0x00000000, 0x00000000, 0x00000000]
26. FAILURE
27: SUCCESS
28: AT END_STRING
30. SUCCESS
----------
nosy: +xtreak
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42550>
_______________________________________
More information about the Python-bugs-list
mailing list