-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython RegEx.py
More file actions
89 lines (69 loc) · 1.77 KB
/
python RegEx.py
File metadata and controls
89 lines (69 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
#RegEx can be used to check if a string contains the specified search pattern.
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
if x:
print("YES! We have a match!")
else:
print("No match")
print(x)
print()
txt = "The rain in Spain"
#Find all lower case characters alphabetically between "a" and "m":
x = re.findall("[a-m]", txt)
print(x)
print()
txt = "That will be 59 dollars"
#Find all digit characters:
x = re.findall("\d", txt)
print(x)
print()
txt = "The rain in Spain falls mainly in the plain!"
#Check if the string contains "ai" followed by 1 or more "x" characters:
x = re.findall("aix+", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
print()
txt = "The rain in Spain"
#Check if "ain" is present at the beginning of a WORD:
x = re.findall(r"\bain", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
print()
xt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)
txt = "The rain in Spain"
x = re.findall("Portugal", txt)
print(x)
txt = "The rain in Spain"
x = re.search("\s", txt)
print("The first white-space character is located in position:", x.start())
txt = "The rain in Spain"
x = re.split("\s", txt)
print(x)
txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)
txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)
txt = "The rain in Spain"
x = re.search("ai", txt)
print(x) #this will print an objecttxt = "The rain in Spain"
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())