I saw this entry.
Use \ A and \ z instead of ^ and $ for regular expression validation
Hmm, I didn't know, I wondered what it was like with python, so I tried it. The version is 3.3.
When I specified \ z like Ruby, it didn't work as expected, so I wondered why, and when I looked at the document, it was \ Z.
>>> import re
>>> re.match('\Aabc\Z', 'abc')
<_sre.SRE_Match object at 0x7f12f3d371d0>
>>> re.match('\Aabc\Z', 'abc\ndef')
>>>
By the way, in Ruby, \ Z and \ z seem to be separated. \ Z seems to judge by ignoring the trailing line break.
It was said that Ruby will be a trap because it is in multi-line mode by default, but python was not so, so I am relieved. Add the re.MULTILINE
option to enter multi-line mode.
>>> re.match('^abc$', 'abc\ndef')
>>>
>>> re.match('^abc$', 'abc\ndef', re.MULTILINE)
<_sre.SRE_Match object at 0x7f12f3d37168>
Note that even if you are not in multi-line mode, $ will match, ignoring the trailing newline. \ Z does not ignore trailing newlines.
>>> re.match('^abc$', 'abc\n')
<_sre.SRE_Match object at 0x7f12f3d371d0>
>>> re.match('\Aabc\Z', 'abc\n')
>>>
I wondered what it was, but it seems that there is no \ A or \ z. (It's true because I just looked it up a little.)
Recommended Posts