import re
# neither S nor s
re.compile(r'[^Ss]+').findall("Some strings.")
['ome ', 'tring', '.']
# not a period
re.compile(r'[^\.]+').findall("work")
['work']
# Either e or ^
re.compile(r'[e^]+').findall("egg, ^")
['e', '^']
# a^b
re.compile(r'[a^b]+').findall("hello, a^b.")
['a^b']
# optional elements
re.compile(r'colou?r').findall("color, colour")
['color', 'colour']
# an integer
re.compile(r'[0-9][0-9]*').findall("2")
['2']
# an integer
re.compile(r'[0-9]+').findall("2")
['2']
# any single character
re.compile(r'beg.n').findall("begin, beg'n, begun")
['begin', "beg'n", 'begun']
# begin and end
# \. means . is a period not athe wildcard
re.compile(r'dog\.$').findall("the dog.")
['dog.']
# boundary
re.compile(r'\bthe\b').findall("other, the, $they")
['the']
# disjunction
re.compile(r'cat|dog').findall("there are a cat and a dog.")
['cat', 'dog']
# precedence
re.compile(r'gupp(y|ies)').findall("guppy and guppies.")
['y', 'ies']
re.compile(r'Column [0-9]+ *').findall("Column 1 Column 2 Column 3 Column 4.")
['Column 1 ', 'Column 2 ', 'Column 3 ', 'Column 4']
# () as a whole
re.compile(r'(Column [0-9]+ *)*').findall("Column 1 Column 2 Column 3 Column 4.")
['Column 4', '', '']
# counters have a higher precedence than sequences, cannot match "theny"
re.compile(r'the|any').findall("the, any, theny")
['the', 'any', 'the']
# mathch word "the"
print(re.compile(r'the').findall("the, The, the_, the25"))
print(re.compile(r'[tT]he').findall("the, their, the_, the25"))
print(re.compile(r'\b[tT]he\b').findall("the, their, the_, the25"))
print(re.compile(r'[^a-zA-Z][tT]he[^a-zA-Z]').findall("the, their, the_, the25"))
print(re.compile(r'(^|[^a-zA-Z])[tT]he([^a-zA-Z]|$)').findall("the, their, the_, the25"))
['the', 'the', 'the'] ['the', 'the', 'the', 'the'] ['the'] [' the_', ' the2'] [('', ','), (' ', '_'), (' ', '2')]
p = re.compile(r'\$[0-9]{0,3}(\.[0-9]+)?\b')
for item in p.finditer("$199.9, one is $199.99. and the other is $199. the last is $1999999.99 . a$199.1"):
print(item)
<_sre.SRE_Match object; span=(0, 6), match='$199.9'> <_sre.SRE_Match object; span=(15, 22), match='$199.99'> <_sre.SRE_Match object; span=(41, 45), match='$199'> <_sre.SRE_Match object; span=(59, 60), match='$'> <_sre.SRE_Match object; span=(74, 80), match='$199.1'>
p = re.compile(r'(^|\W)\$[0-9]{0,3}(\.[0-9]+)?\b')
for item in p.finditer("$199.9, one is $199.99. and the other is $199. the last is $1999999.99 . a$199.1"):
print(item)
<_sre.SRE_Match object; span=(0, 6), match='$199.9'> <_sre.SRE_Match object; span=(14, 22), match=' $199.99'> <_sre.SRE_Match object; span=(40, 45), match=' $199'> <_sre.SRE_Match object; span=(58, 60), match=' $'>
for item in re.compile(r'the (.*)er they were, the \1er they will be').finditer(
"the bigger they were, the bigger they will be but not the bigger they were, the faster they will be."):
print(item)
<_sre.SRE_Match object; span=(0, 45), match='the bigger they were, the bigger they will be'>
for item in re.compile(r'the (.*)er they (.*), the \1er we \2').finditer(
"the faster they ran, the faster we ran but not the faster they ran, the faster we ate."):
print(item)
<_sre.SRE_Match object; span=(0, 38), match='the faster they ran, the faster we ran'>
# non-capturing
for item in re.compile(r'(?:some|a few) (people|cats) like some \1').finditer(
"a few cats like some cats but not some cats like some a few."):
print(item)
<_sre.SRE_Match object; span=(0, 25), match='a few cats like some cats'>
print(re.match("([abc])+", "abc").group())
print(re.match("(?:[abc])+", "abc").group())
abc abc
print(re.match("([abc])+", "abc").groups())
print(re.match("(?:[abc])+", "abc").groups())
('c',) ()
# 前向
test = re.compile(r'^(?=Volcano)[a-zA-Z]+')
print(test.findall("Volcano I"))
test = re.compile(r'^(?!Volcano)[a-zA-Z]+')
print(test.findall("Volcano I"))
['Volcano'] []
# 后向
test = re.compile(r'^(?<=Volcano)[a-zA-Z]+')
print(test.findall("Volcano I"))
test = re.compile(r'^(?<!Volcano)[a-zA-Z]+')
print(test.findall("Volcano I"))
[] ['Volcano']