>>> import re
>>> re.match("\\w{5,20}", "v.pupkin")
>>> len("\\\\")
2
>>> re.match("\\\\", "\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>
>>> bool(re.match("\\\\", "\\"))
True
>>> "\\\\"
'\\\\'
>>> print("\\\\")
\\
>>> re.match("\\\\", "\\\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>
>>> bool(re.match("\\\\", "\\\\"))
True
>>> bool(re.match("^\\\\$", "\\\\"))
False
>>> bool(re.match("^\\\\$", "\\"))
True
>>> bool(re.match("\\\\", "\\\\"))
True
>>> bool(re.match("^\\\\$", "\\"))
True
>>> bool(re.match("^\\\\$", "\\\\"))
False
>>> bool(re.match("\\\\", "\\\\\\\\\\\\"))
True
>>> bool(re.match("devil", "He is a devil"))
False
>>> bool(re.match(".*devil.*", "He is a devil"))
True
>>> bool(re.match(".*devil", "He is a devil"))
True
>>> bool(re.match("devil", "He is a devil"))
False
>>> bool(re.match("devil", "He is a devil "))
False
>>> bool(re.match("devil", "devil "))
True
>>> help(re.match)
>>> bool(re.fullmatch("\\w{5,20}", "v.pupkin"))
False
>>> bool(re.fullmatch("\\w{5,20}", "v_pupkin"))
True
>>> print("\\w{5,20}")
\w{5,20}
>>> bool(re.fullmatch(".*devil.*", "He is a devil."))
True
>>> bool(re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."))
True
>>> bool(re.fullmatch(".*devil|hell|daemon.*", "He is a devil."))
False
>>> bool(re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."))
True
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.")
<_sre.SRE_Match object; span=(0, 14), match='He is a devil.'>
>>> if re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."):
... print("Shut up")
...
Shut up
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.")
<_sre.SRE_Match object; span=(0, 14), match='He is a devil.'>
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").groups()
('devil',)
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").group(1)
'devil'
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").groups()
('devil',)
>>> re.fullmatch(".*(devil|hell|daemon).*", "Go to hell. He is a devil.").groups()
('devil',)
>>> re.fullmatch(".*(devil|hell|daemon).*", "Go to hell. He is a devil. Go to hell.").groups()
('hell',)
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil. Go to hell.").groups()
('hell',)
>>> re.fullmatch("a(b*)c(d*)", "abbc").groups()
('bb', '')
>>> re.fullmatch("(\\S+)@(\\S+)", "[email protected]").groups()
('mikhail.dvorkin', 'gmail.com')
>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil. Go to hell.").groups()
('hell',)
>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
<_sre.SRE_Match object; span=(8, 13), match='devil'>
>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
()
>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
<_sre.SRE_Match object; span=(8, 13), match='devil'>
>>> list(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '_sre.SRE_Match' object is not iterable
>>> dir(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
()
>>> re.search("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
('devil',)
>>> re.findall("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'groups'
>>> re.findall("(devil|hell|daemon)", "He is a devil. Go to hell.")
['devil', 'hell']
>>> re.findall("devil|hell|daemon", "He is a devil. Go to hell.")
['devil', 'hell']
>>> len(re.findall("devil|hell|daemon", "He is a devil. Go to hell."))
2
>>> len(re.findall("devil|hell|daemon", "He is a devil. Go to hell."))
2
>>> re.sub("devil|hell|daemon", "He is a devil. Go to hell.", "***")
'***'
>>> re.sub("devil|hell|daemon", "***", "He is a devil. Go to hell.")
'He is a ***. Go to ***.'
>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
<_sre.SRE_Match object; span=(8, 13), match='devil'>
>>> bool(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
True
>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
()
>>> re.search("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
('devil',)
Comments