Regular Expressions in Python
Regular Expressions (regex) are sequences of characters used to match patterns in text. They help
search, validate, extract, and replace text. Python supports regex through the re module.
Why Regular Expressions?
They help validate emails, phone numbers, extract numbers, clean data, and perform complex text
matching.
Common Symbols:
\d – digit
\w – letter, digit, underscore
\s – space
. – any character
+ – one or more
* – zero or more
? – optional
^ – start of string
$ – end of string
Regex Functions:
1. [Link]() – finds first match
2. [Link]() – returns all matches
3. [Link]() – checks only beginning of text
4. [Link]() – replace matching text
Example 1: Email Extraction
pattern = r"\w+@\w+\.\w+"
text = "my email is abc123@[Link]"
Output: ['abc123@[Link]']
Example 2: Extracting Numbers
[Link](r"\d+", "Marks: 78, 88, 92")
Output: ['78', '88', '92']
Example 3: Replace Digits
[Link](r"\d", "*", "Call 98765")
Output: 'Call *****'
Conclusion:
Regular expressions provide powerful tools for searching, validating, and processing text efficiently
in Python.