This repository was archived by the owner on Jul 10, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy path01_reModule.py
More file actions
217 lines (150 loc) · 7.09 KB
/
Copy path01_reModule.py
File metadata and controls
217 lines (150 loc) · 7.09 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
# coding: utf-8
# ================================================================
# EXECUTIVE SUMMARY : re module** for working with regular expressions
# ================================================================
# This script demonstrates Python's **re module** for working with regular expressions.
# The re module in Python is a built-in library for working with regular expressions (regex). Regular expressions are sequences of characters that define search patterns, enabling tasks such as text searching, extraction, and manipulation.
# Purpose:
# - To showcase how regex can be used for text searching, replacement, and pattern matching.
# - To provide recruiter-friendly explanations of regex syntax and its practical applications.
# Why it matters:
# - Regular expressions are essential for text processing tasks such as cleaning data,
# validating inputs, and extracting information.
# - Recruiters value candidates who can explain both the *how* and the *why* of regex usage.
# Techniques highlighted:
# - `re.sub()` for substitution.
# - `re.search()` for pattern matching.
# - Regex metacharacters: `\d`, `\D`, `\w`, `\W`, `\s`, `\S`, `^`, `$`, `[ ]`, `[^ ]`.
"""re.sub(pattern, repl, string, count=0, flags=0):
pattern: The regular expression to match.
repl: The replacement string.
string: The input string.
count: Optional; the maximum number of replacements to make. Default is 0, which means all occurrences are replaced.
flags: Optional; flags to modify the behavior of the regex.
"""
# ================================================================
# ==== Step 1: Import re Module ====
import re
# ==== Step 2: Basic Substitution ====
sentence = 'Hi, I am sachin'
new_sen = re.sub(r'sachin', 'rahul', sentence)
print(new_sen)
# Explanation:
# - `re.sub(pattern, repl, string)` replaces occurrences of `pattern` with `repl`.
# - Here, "sachin" is replaced with "rahul".
# ==== Step 3: Case-Insensitive Replacement ====
Ignor = re.sub(r'sachin', 'Ajinkya', sentence, flags=re.IGNORECASE)
print(Ignor)
# Explanation:
# - `flags=re.IGNORECASE` makes the match case-insensitive.
# ==== Step 4: Digit Replacement ====
info = 'Sachin is from Nagpur have 2 bikes and 4 bicycle'
print(info)
new_info = re.sub(r'\d', '#', info)
print(new_info)
# Explanation:
# - `\d` matches any digit (0–9).
# - All digits are replaced with "#".
new_info1 = re.sub(r'\D', '#', info)
print(new_info1)
# - `\D` matches any non-digit character.
new_info2 = re.sub(r'[0-9]', '*', info)
print(new_info2)
# - `[0-9]` explicitly matches digits.
# ==== Step 5: Word and Non-Word Characters ====
new_info3 = re.sub(r'\w', '#', info)
print(new_info3)
# - `\w` matches word characters (letters, digits, underscores).
# function replaces each word character (\w) with the character #.
# \w: Matches word characters (letters, digits, and underscores).
# Using \w is useful when you want to replace all alphanumeric characters and underscores in a string.
new_info4 = re.sub(r'\W', '#', info)
print(new_info4)
# - `\W` matches non-word characters (spaces, punctuation).
# ==== Step 6: Whitespace and Non-Whitespace ====
new_info5 = re.sub(r'\s', '#', info)
print(new_info5)
# - `\s` matches whitespace characters.
# r'\s': This is a regular expression pattern that matches any whitespace character, including spaces, tabs, newlines, etc.
# Each matched whitespace character is replaced with the specified replacement string, which is # in this case.
new_info6 = re.sub(r'\S', '#', info)
print(new_info6)
# - `\S` matches non-whitespace characters.
# r'\S': This is a regular expression pattern that matches any non-space character.
# The \S pattern matches any single non-space character in the string.
# Non-space characters include letters, numbers, punctuation, and special characters.
# ==== Step 7: Character Classes ====
new_info7 = re.sub(r'[a-z]', '#', info)
print(new_info7)
# - `[a-z]` matches lowercase letters.
new_info8 = re.sub(r'[a-zA-Z]', '#', info)
print(new_info8)
# - `[a-zA-Z]` matches both lowercase and uppercase letters.
new_info9 = re.sub(r'[a-zA-Z0-9]', '#', info)
print(new_info9)
# - `[a-zA-Z0-9]` matches alphanumeric characters.
# ==== Step 8: Specific Character Matching ====
new_sen = re.sub(r'[rw]', '#', info)
print(new_sen)
# - `[rw]` matches "r" or "w".
new_sen1 = re.sub(r'[^rw]', '#', info)
print(new_sen1)
# - `[^rw]` matches any character except "r" or "w".
# ==== Step 9: Searching Patterns ====
new_sen2 = re.search(r'from', info)
print(new_sen2)
# - `re.search()` finds the first occurrence of "from".
# ==== Step 10: Empty String Matching ====
unique = ""
sen_unique = re.search(r'.*', unique)
print(sen_unique)
# - `.*` matches zero or more characters (including empty string).
sen_unique1 = re.search(r'.+', unique)
print(sen_unique1)
# - `.+` requires at least one character (fails on empty string).
# The .+ pattern matches one or more characters.
# This means it cannot match an empty string.
# ==== Step 11: Anchors (^ and $) ====
sen_search = re.search(r'^r', info)
print(sen_search)
# - `^r` matches "r" at the start of the string.
sen_search1 = re.search(r'^S', info)
print(sen_search1)
# - `^S` matches "S" at the start of the string.
# r'^S': This is a regular expression pattern that matches the character "r" only if it is at the beginning of the string.
# The ^ symbol indicates the start of the string.
sen_search2 = re.search(r'2$', info)
print(sen_search2)
# - `2$` matches "2" at the end of the string.
# r'2$': This is a regular expression pattern that matches the digit "2" only if it is at the end of the string.
# The $ symbol indicates the end of the string.
sen_search3 = re.search(r'bicycle$', info)
print(sen_search3)
# - `bicycle$` matches "bicycle" at the end of the string.
# r'bicycle$': This is a regular expression pattern that matches the word "bicycle" only if it is at the end of the string.
# The $ symbol indicates the end of the string.
# ==== Step 12: Advanced Substitution ====
Info2 = "Hello, I am sachin $ 30 years old and have 2 dogs."
sen_search4 = re.sub(r'sachin', 'Ashwin', Info2)
print(sen_search4)
# - Replace "sachin" with "Ashwin".
print(re.sub(r'^', '#', Info2))
# - Adds "#" at the start of the string.
# re.sub(r'\^', '#', info2) does nothing if there are no caret characters in the string
sen_search5 = re.sub(r'\$', '#', Info2)
print(sen_search5)
# Each matched dollar sign is replaced with the specified replacement string, which is # in this case.
# ================================================================
# FINAL SUMMARY
# ================================================================
# - We explored Python's `re` module for regex operations.
# - Key functions: `re.sub()` for substitution, `re.search()` for pattern matching.
# - Regex patterns demonstrated:
# * `\d`, `\D` → digits vs non-digits
# * `\w`, `\W` → word vs non-word characters
# * `\s`, `\S` → whitespace vs non-whitespace
# * `[ ]`, `[^ ]` → character classes and negation
# * `^`, `$` → anchors for start/end of string
# - Practical applications: text cleaning, anonymization, validation, and extraction.
# ================================================================