Don't use \ d in Python 3 regular expressions!

It seems that it is not known unexpectedly, but in the regular expression of the Python 3 standard re module, \ d also matches so-called double-byte numbers.

I will actually try it.

python


>>> import re
>>> re.findall(r"\d", "012012")
['0', '1', '2', '0', '1', '2']
>>> 

\ d also matches '0', '1', and '2'.

The reason why this behavior is not well known is

There may be other reasons as well.

The re module documentation also states that it is recommended to use [0-9] instead of \ d to match \ d only to [0-9]. , but for reasons such as wanting to use the long regular expressions for other languages as they are, \ If you want to keep d, you can either add flags = re.ASCII to the argument or add(? A)to the beginning of the regular expression.

python


>>> import re
>>> re.findall(r"\d", "012012", flags=re.ASCII)
['0', '1', '2']
>>> re.findall(r"(?a)\d", "012012")
['0', '1', '2']
>>> 

However, these flags affect the entire regular expression. For more information, please read the re module documentation.

Note that flags = is omitted. You can write it as re.findall (r" \ d "," 012012 ", re.ASCII), but if you omit it poorly, you may get hooked, so it is strongly recommended not to omit it.

By the way, I myself

For reasons such as

python


import regex

RE_DIGITS = regex.compile(r"""(?xa)
    \A\d+\Z""")


def is_digits(digits):
    if RE_DIGITS.match(digits) is not None:
        return True
    else:
        return False

I like to write like this, but I still feel uneasy when using \ d, so I try to write[0-9]as much as possible. (ʻIs_digits () `is just an example, so just in case)

Recommended Posts

Don't use \ d in Python 3 regular expressions!
Use regular expressions in Python
How to use regular expressions in Python
Use regular expressions in C
When using regular expressions in Python
[Python] Regular Expressions Regular Expressions
Overlapping regular expressions in Python and Java
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Regular expression in Python
Regular expression in Python
Pharmaceutical company researchers summarized regular expressions in Python
Don't use readlines () in your Python for statement!
Multiple regression expressions in Python
Let's use def in python
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use Random Forest in Python
Don't make test.py in Python!
Use Spyder in Python IDE
Wrap long expressions in python
About Python and regular expressions
Extract arbitrary strings using Python regular expressions / Use named groups
Use fabric as is in python (fabric3)
How to use SQLite in Python
I can't remember Python regular expressions
Use rospy with virtualenv in Python3
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Use OpenCV with Python 3 in Window
Handling regular expressions with PHP / Python
A memo that handles double-byte double quotes in Python regular expressions
Regular expressions that are easy and solid to learn in Python
[Python, Julia] 3D display in Jupyter-Mayavi library
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
ABC 157 D --Solve Friend Suggestions in Python!
Easily use your own functions in Python
Start / end match in python regular expression
Easy way to use Wikipedia in Python
How to use __slots__ in Python class
Solve ABC165 A, B, D in Python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
[Road to intermediate Python] Use lambda expressions
Python: Simplified morphological analysis with regular expressions
How to use is and == in Python
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