-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregexT.py
More file actions
121 lines (68 loc) · 6.82 KB
/
Copy pathregexT.py
File metadata and controls
121 lines (68 loc) · 6.82 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# ===================== SUMMARY OF re MODULE =====================
# re module is used for Regular Expressions in Python.
# Regular Expression means searching/finding a pattern inside a string.
# re.search() checks pattern anywhere in the string.
# re.match() checks pattern only at the beginning of the string.
# re.fullmatch() checks whether the whole string matches the pattern.
# re.findall() returns all matched values as a list.
# re.finditer() returns match objects one by one.
# re.sub() replaces matched pattern with another value.
# re.split() splits string based on a pattern.
# re.compile() stores a pattern so we can reuse it again and again.
# ===============================================================
import re # importing re module for regular expressions
text = "My name is Saad and my age is 22" # creating sample text
result = re.search("Saad", text) # searching "Saad" anywhere inside text
print(result.group()) # prints matched text | Output: Saad
print(result.start()) # prints starting index of match | Output: 11
print(result.end()) # prints ending index of match | Output: 15
result = re.search("Delhi", text) # searching "Delhi" inside text
print(result is None) # checks if no match found | Output: True
result = re.match("My", text) # checking if text starts with "My"
print(result.group()) # prints matched starting text | Output: My
result = re.match("Saad", text) # checking if text starts with "Saad"
print(result is None) # checks if match failed | Output: True
result = re.fullmatch("My name is Saad and my age is 22", text) # checking if full text matches exactly
print(result is not None) # checks if full match happened | Output: True
numbers = re.findall(r"\d+", text) # finding all continuous numbers from text
print(numbers) # prints all numbers as list | Output: ['22']
words = re.findall(r"[A-Za-z]+", text) # finding all alphabet words from text
print(words) # prints all words as list | Output: ['My', 'name', 'is', 'Saad', 'and', 'my', 'age', 'is']
for match in re.finditer(r"\d+", text): # finding numbers one by one as match objects
print(match.group()) # prints matched number | Output: 22
print(match.start()) # prints start index of number | Output: 31
print(match.end()) # prints end index of number | Output: 33
new_text = re.sub(r"\d+", "25", text) # replacing full number with 25
print(new_text) # prints updated text | Output: My name is Saad and my age is 25
hidden_text = re.sub(r"\d", "*", text) # replacing each digit with star
print(hidden_text) # prints hidden digit text | Output: My name is Saad and my age is **
sentence = "apple,banana;orange grape" # creating sentence with different separators
parts = re.split(r"[,;\s]", sentence) # splitting by comma, semicolon, or space
print(parts) # prints split list | Output: ['apple', 'banana', 'orange', 'grape']
email = "saad123@gmail.com" # creating email string
result = re.search(r"\w+@\w+\.\w+", email) # searching basic email pattern
print(result.group()) # prints matched email | Output: saad123@gmail.com
mobile = "9876543210" # creating mobile number string
result = re.fullmatch(r"\d{10}", mobile) # checking exactly 10 digits
print(result.group()) # prints valid mobile number | Output: 9876543210
password = "Saad@123" # creating password string
print(re.search(r"[A-Z]", password).group()) # prints first uppercase letter | Output: S
print(re.search(r"[a-z]", password).group()) # prints first lowercase letter | Output: a
print(re.search(r"\d", password).group()) # prints first digit | Output: 1
print(re.search(r"[@#$%]", password).group()) # prints first special character | Output: @
name = "Saad Arfin" # creating full name string
result = re.search(r"(\w+)\s(\w+)", name) # grouping first name and last name
print(result.group()) # prints full matched name | Output: Saad Arfin
print(result.group(1)) # prints first group | Output: Saad
print(result.group(2)) # prints second group | Output: Arfin
date = "28-06-2026" # creating date string
result = re.search(r"(?P<day>\d{2})-(?P<month>\d{2})-(?P<year>\d{4})", date) # making named groups for day, month, year
print(result.group("day")) # prints day from date | Output: 28
print(result.group("month")) # prints month from date | Output: 06
print(result.group("year")) # prints year from date | Output: 2026
text2 = "python is easy. PYTHON is powerful." # creating another sample text
print(re.findall(r"python", text2)) # finds lowercase python only | Output: ['python']
print(re.findall(r"python", text2, re.I)) # finds python ignoring case | Output: ['python', 'PYTHON']
pattern = re.compile(r"\d+") # compiling number pattern for reuse
print(pattern.findall("Marks are 80 and 90")) # finds all numbers using compiled pattern | Output: ['80', '90']
print(pattern.sub("XX", "Marks are 80 and 90")) # replaces all numbers using compiled pattern | Output: Marks are XX and XX