[PYTHON] When writing an if statement with a regular expression

I often forget it, so make a note

Found by partial match Match if any of the patterns on the left hits the right. It would be strange to write'|' outside the pattern. When you write parentheses in a pattern, you have to escape It's the opposite of perl and a little confusing.


import re
if re.compile("Fukuoka|Kanagawa|Tokyo|Chiba").search("Tokyo都目黒区"):
    print("Match!")

Other methods in .search

match(str) Determines if the beginning of the string matches, returns a MatchObject instance if it matches, returns None if it doesn't. findall(str) Returns a list of MatchObject instances with all the matching parts. Returns an empty list if there is no match finditer(str) Returns all matching parts in the Iterator of the MatchObject instance. Returns an empty iterator if it doesn't match

Postscript: Each of the following matches one

if "Tokyo" in ['Fukuoka','Kanagawa','Tokyo','Tokyo都','Chiba']:
    print('Match!')

if "Tokyo" in 'Tokyo都品川区':
    print('Match!')

When perl By the way, it was like this. I think there were some notes when I was in Japanese ... I forgot. ..


if ("Hello, world!" =~ /wor/) {
    print("Match!\n");
}

Reference: Introduced in various codes. https://hydrocul.github.io/wiki/programming_languages_diff/string/match.html

Recommended Posts

When writing an if statement with a regular expression
Determine if a string is a time with a python regular expression
Regular expression with pymongo
If you get a long error when tabbing an interactive shell with Anaconda
Make one repeating string with a Python regular expression.
A memorandum when an error occurs with pip install
Addition with Python if statement
Regular expression manipulation with Python
Turn an array of strings with a for statement (Python3)
Get the matched string with a regular expression and reuse it when replacing on Python3
[Python] What is a with statement?
Decompose hostname with co.jp with regular expression
String replacement with Python regular expression
When writing a program in Python
What to do if an error occurs when importing numpy with VScode
Note when creating an environment with python
When I get an error with PyInstaller
Swap 1 and 2 without using an if statement
A workaround when installing pyAudio with pip.
Operate an oscilloscope with a Raspberry Pi
What to do if an error occurs when loading a python project created with poetry into VS Code
Workaround if you get an error when trying to install PySide with pip
It is more convenient to use csv-table when writing a table with python-sphinx
What to do if you get an error when installing python with pyenv
Error when installing a module with Python pip
I got an error when saving with OpenCV
[Python] Make a game with Pyxel-Use an editor-
Make a monitoring device with an infrared sensor
I get a UnicodeDecodeError when running with mod_wsgi
Generate an insert statement from CSV with Python.
A memo when creating a python environment with miniconda
Rollback processing when an error occurs with fabric
[Python] I want to use only index when looping a list with a for statement
What to do if you get an OpenSSL error when installing Python 2 with pyenv
Writing notes when running a normal python executable (file containing argparse) with Jupyter notebook
What to do if you get an Import Error when importing matplotlib with Jupyter
A story about an error when loading a TensorFlow model created with Google Colab locally