Start / end match in python regular expression

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.

\ Z (uppercase) instead of \ z (lowercase)

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.

Default is not multi-line mode

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>

Handling of trailing line breaks

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')
>>>

In javascript ...

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

Start / end match in python regular expression
Regular expression in Python
Regular expression in Python
Regular expression in regex.h
python regular expression memo
Achieve __END__ in Python
Regular expression symbolic group name in Python / Ruby
Use let expression in Python
Python 處 處 regular expression Notes
[Gimp] Start scripting in Python
Use regular expressions in Python
Regular expression manipulation with Python
A python regular expression, or a memo of a match object
Reproduce the Python regular expression r'\ w (? U)' in JavaScript
String replacement with Python regular expression
When using regular expressions in Python
Start python
Overlapping regular expressions in Python and Java
Python3> in keyword> True with partial match?
(Python) HTML reading and regular expression notes
Replace non-ASCII with regular expressions in Python
Don't use \ d in Python 3 regular expressions!
How to use regular expressions in Python
URL match checking and extraction with python regular expression Regex Full version
Features of regular expression modules that I often use personally in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Regular expression Greedy
Pharmaceutical company researchers summarized regular expressions in Python
Match the distribution of each group in Python
Epoch in Python
Discord in Python
Start using Python
Python quick start
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
How to write regular expression patterns in Linux
N-Gram in Python
[Python] Start studying
Programming in python
Plink in Python
[Python] Regular Expressions Regular Expressions
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python