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