-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path68_Python_RegEx.py
59 lines (46 loc) · 1000 Bytes
/
68_Python_RegEx.py
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
# Python RegEx
print("Python RegEx")
print("\nRegEx in Python")
# regex module
import re
# Example 1
print("Example 1")
# Checking if a string starts with "The" and ends with "Spain"
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
if x:
print("Yes, we have a match.")
else:
print("No match.")
# regex functions
print("\nRegEx Functions\n")
print("findall()")
y = re.findall("ai", txt)
print(y)
print("")
print("search()")
z = re.search("\s", txt)
# print(type(z))
print(z.start())
print("")
print("split()")
# Spliting the string at every white-space character
m = re.split("\s", txt)
print(m)
print("")
print("sub()")
# Replacing every white-space character with underscore
n = re.sub("\s", "_", txt)
print(n)
# Match Object
print("\nMatch Object")
print("\nExample 1")
txt2 = "This is Some text."
o = re.search("text", txt2)
print(o)
print("\nExample 2")
d = re.search(r"\bS\w", txt2)
print(d.span())
print("\nExample 3")
e = re.search(r"\bS\w", txt2)
print(e.string)