· 9 years ago · Dec 05, 2016, 01:14 PM
1>>> import re
2>>> re.match("\\w{5,20}", "v.pupkin")
3>>> len("\\\\")
42
5>>> re.match("\\\\", "\\")
6<_sre.SRE_Match object; span=(0, 1), match='\\'>
7>>> bool(re.match("\\\\", "\\"))
8True
9>>> "\\\\"
10'\\\\'
11>>> print("\\\\")
12\\
13>>> re.match("\\\\", "\\\\")
14<_sre.SRE_Match object; span=(0, 1), match='\\'>
15>>> bool(re.match("\\\\", "\\\\"))
16True
17>>> bool(re.match("^\\\\$", "\\\\"))
18False
19>>> bool(re.match("^\\\\$", "\\"))
20True
21>>> bool(re.match("\\\\", "\\\\"))
22True
23>>> bool(re.match("^\\\\$", "\\"))
24True
25>>> bool(re.match("^\\\\$", "\\\\"))
26False
27>>> bool(re.match("\\\\", "\\\\\\\\\\\\"))
28True
29>>> bool(re.match("devil", "He is a devil"))
30False
31>>> bool(re.match(".*devil.*", "He is a devil"))
32True
33>>> bool(re.match(".*devil", "He is a devil"))
34True
35>>> bool(re.match("devil", "He is a devil"))
36False
37>>> bool(re.match("devil", "He is a devil "))
38False
39>>> bool(re.match("devil", "devil "))
40True
41>>> help(re.match)
42
43>>> bool(re.fullmatch("\\w{5,20}", "v.pupkin"))
44False
45>>> bool(re.fullmatch("\\w{5,20}", "v_pupkin"))
46True
47>>> print("\\w{5,20}")
48\w{5,20}
49>>> bool(re.fullmatch(".*devil.*", "He is a devil."))
50True
51>>> bool(re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."))
52True
53>>> bool(re.fullmatch(".*devil|hell|daemon.*", "He is a devil."))
54False
55>>> bool(re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."))
56True
57>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.")
58<_sre.SRE_Match object; span=(0, 14), match='He is a devil.'>
59>>> if re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."):
60... print("Shut up")
61...
62Shut up
63>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.")
64<_sre.SRE_Match object; span=(0, 14), match='He is a devil.'>
65>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").groups()
66('devil',)
67>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").group(1)
68'devil'
69>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").groups()
70('devil',)
71>>> re.fullmatch(".*(devil|hell|daemon).*", "Go to hell. He is a devil.").groups()
72('devil',)
73>>> re.fullmatch(".*(devil|hell|daemon).*", "Go to hell. He is a devil. Go to hell.").groups()
74('hell',)
75>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil. Go to hell.").groups()
76('hell',)
77>>> re.fullmatch("a(b*)c(d*)", "abbc").groups()
78('bb', '')
79>>> re.fullmatch("(\\S+)@(\\S+)", "mikhail.dvorkin@gmail.com").groups()
80('mikhail.dvorkin', 'gmail.com')
81>>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil. Go to hell.").groups()
82('hell',)
83>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
84<_sre.SRE_Match object; span=(8, 13), match='devil'>
85>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
86()
87>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
88<_sre.SRE_Match object; span=(8, 13), match='devil'>
89>>> list(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
90Traceback (most recent call last):
91 File "<stdin>", line 1, in <module>
92TypeError: '_sre.SRE_Match' object is not iterable
93>>> dir(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
94['__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']
95>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
96()
97>>> re.search("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
98('devil',)
99>>> re.findall("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
100Traceback (most recent call last):
101 File "<stdin>", line 1, in <module>
102AttributeError: 'list' object has no attribute 'groups'
103>>> re.findall("(devil|hell|daemon)", "He is a devil. Go to hell.")
104['devil', 'hell']
105>>> re.findall("devil|hell|daemon", "He is a devil. Go to hell.")
106['devil', 'hell']
107>>> len(re.findall("devil|hell|daemon", "He is a devil. Go to hell."))
1082
109>>> len(re.findall("devil|hell|daemon", "He is a devil. Go to hell."))
1102
111>>> re.sub("devil|hell|daemon", "He is a devil. Go to hell.", "***")
112'***'
113>>> re.sub("devil|hell|daemon", "***", "He is a devil. Go to hell.")
114'He is a ***. Go to ***.'
115>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
116<_sre.SRE_Match object; span=(8, 13), match='devil'>
117>>> bool(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
118True
119>>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
120()
121>>> re.search("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
122('devil',)