1import re
2
3# Regex Cheat sheet : https://www.dataquest.io/blog/regex-cheatsheet/
4# Regex python tester : https://pythex.org/
5# re doc : https://docs.python.org/3/library/re.html
6
7text = "i like train"
8reg = r"[a-c]" #the group of char a to c
9
10if re.match(reg, text): #Check if regex is correct
11 print(text)
12else:
13 print("Not any match")
1import re
2# Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods, described below.
3
4prog = re.compile(pattern)
5result = prog.match(string)
6
7# is equivalent to
8
9result = re.match(pattern, string)
1import re
2
3# The string you want to find a pattern within
4test_string = 'Hello greppers!'
5
6# Creating a regular expression pattern
7# This is a simple one which finds "Hello"
8pattern = re.compile(r'Hello')
9
10# This locates and returns all the occurences of the pattern
11# within the test_string
12match = pattern.finditer(test_string)
13
14# Outputs all the ocurrences which were returned as
15# as match objects
16for match in matches:
17 print(match)
18
19
1# pip install regex
2import re
3# simple find all
4sample = "Nothing lasts... but nothing is lost"
5found = re.findall("thing", sample)
6print(found)