"""
When using a regular expression for a Japanese character string
String class of Japanese strings(str or unicode)And the regular expression string class needs to match
Example) re.match([If this is unicode], [This is also unicode])
"""
str_japanese = '3 mana creature'
unicode_japanese = u'Three mana creature'
match = re.search('Mana', str_japanese)
if match:
print "Apply regular expression to str without u flag"
match = re.search(u'Mana', str_japanese)
if match:
print "Assign the u flag to the regular expression for str"
match = re.search('Mana', unicode_japanese)
if match:
print "Apply regular expression to unicode without u flag"
match = re.search(u'Mana', unicode_japanese)
if match:
print "Assign the u flag to the regular expression for unicode"
#By the way, the r flag is treated as str
match = re.search(r'Mana', str_japanese)
if match:
print "Apply r flag to regular expression for str"
match = re.search(r'Mana', unicode_japanese)
if match:
print "Apply r flag to regular expression for unicode"
"""
output(Only the part that matches properly is printed)
>>>Apply regular expression to str without u flag
>>>Assign the u flag to the regular expression for unicode
>>>Apply r flag to regular expression for str
"""
Recommended Posts