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
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
1import re
2
3pattern = '^a...s$'
4test_string = 'abyss'
5result = re.match(pattern, test_string)
6
7if result:
8 print("Search successful.")
9else:
10 print("Search unsuccessful.")
11
1# pip install regex
2import re
3# simple find all
4sample = "Nothing lasts... but nothing is lost"
5found = re.findall("thing", sample)
6print(found)