regular expression using python 3
re.match(pattern, "spamspamspam")
#returns True
**re.sub(pattern, repl, string, count=0)**
str = "My name is David. Hi David."
pattern = r"David"
newstr = re.sub(pattern, "Amy", str)
print(newstr)
>>>
My name is Amy. Hi Amy.
>>>
pattern = r"^gr.y$"
pattern = r"[^A-Z]"
if re.search(pattern, "this is all quiet"):
print("Match 1")
if re.search(pattern, "AbCdEfG123"):
print("Match 2")
if re.search(pattern, "THISISALLSHOUTING"):
print("Match 3")
##The pattern [^A-Z] excludes uppercase strings.
Note, that the ^ should be inside the brackets to invert the character class.
>>>
Match 1
Match 2
>>>
pattern = r"ice(-)?cream"
if re.match(pattern, "ice-cream"):
print("Match 1")
if re.match(pattern, "icecream"):
print("Match 2")
pattern = r"9{1,3}$"
if re.match(pattern, "9"):
print("Match 1")
if re.match(pattern, "999"):
print("Match 2")
if re.match(pattern, "9999"):
print("Match 3")
>>>
Match 1
Match 2
>>>
pattern = r"(.+) \1"
match = re.match(pattern, "word word")
if match:
print ("Match 1")
match = re.match(pattern, "?! ?!")
if match:
print ("Match 2")
match = re.match(pattern, "abc cde")
if match:
print ("Match 3")
>>>
Match 1
Match 2
>>>
Note, that "(.+) \1" is not the same as "(.+) (.+)", because \1 refers to the first group's subexpression, which is the matched expression itself, and not the regex pattern.
if re.search(pattern, "eggspamsausagespam"):
print("Match")
else:
print("No match")
print(re.findall(pattern, "eggspamsausagespam"))
>>>
Match
['spam', 'spam']
>>>
pattern = r"gr.y"
# this will be grey or gray or anything else except newline
pattern = r"[A-Z][A-Z][0-9]"
if re.search(pattern, "LS8"):
print("Match 1")
if re.search(pattern, "E3"):
print("Match 2")
#The pattern in the example above matches strings that contain two alphabetic uppercase letters followed by a digit.
>>>
Match 1
>>>
pattern = r"g+"
if re.match(pattern, "g"):
print("Match 1")
To summarize:
* matches 0 or more occurrences of the preceding expression.
+ matches 1 or more occurrence of the preceding expression.
pattern = r"a(bc)(de)(f(g)h)i"
match = re.match(pattern, "abcdefghijklmnop")
if match:
print(match.group())
print(match.group(0))
print(match.group(1))
print(match.group(2))
print(match.groups())
>>>
abcdefghi
abcdefghi
bc
de
('bc', 'de', 'fgh', 'g')
>>>
pattern = r"(\D+\d)"
match = re.match(pattern, "Hi 999!")
if match:
print("Match 1")
match = re.match(pattern, "1, 23, 456!")
if match:
print("Match 2")
match = re.match(pattern, " ! $?")
if match:
print("Match 3")
>>>
Match 1
>>>
match = re.search(pattern, "eggspamsausage")
if match:
print(match.group())
print(match.start())
print(match.end())
print(match.span())
>>>
pam
4
7
(4, 7)
>>>
pattern = r"[aeiou]"
if re.search(pattern, "grey"):
print("Match 1")
if re.search(pattern, "qwertyuiop"):
print("Match 2")
if re.search(pattern, "rhythm myths"):
print("Match 3")
#The pattern [aeiou] in the search function matches all strings that contain any one of the characters defined
>>>
Match 1
Match 2
>>>
pattern = r"egg(spam)*"
if re.match(pattern, "egg"):
print("Match 1")
if re.match(pattern, "eggspamspamegg"):
print("Match 2")
if re.match(pattern, "spam"):
print("Match 3")
>>>
match 1
match 2
>>>
The example above matches strings that start with "egg" and follow with zero or more "spam"s.
................................................
pattern = r"(?P<first>abc)(?:def)(ghi)"
match = re.match(pattern, "abcdefghi")
if match:
print(match.group("first"))
print(match.groups())
>>>
abc
('abc', 'ghi')
>>>
pattern = r"gr(a|e)y"
match = re.match(pattern, "gray")
if match:
print ("Match 1")
match = re.match(pattern, "grey")
if match:
print ("Match 2")
match = re.match(pattern, "griy")
if match:
print ("Match 3")
>>>
Match 1
Match 2
>>>
pattern = r"\b(cat)\b"
match = re.search(pattern, "The cat sat!")
if match:
print ("Match 1")
match = re.search(pattern, "We s>cat<tered?")
if match:
print ("Match 2")
match = re.search(pattern, "We scattered.")
if match:
print ("Match 3")
>>>
Match 1
Match 2
>>>
"\b(cat)\b" basically matches the word "cat" surrounded by word boundaries.
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.