
![]() |
@wtf | |
(Find emails & phone numbers) |
||
1
Replies
6
Views
1 Bookmarks
|
![]() |
@wtf | 3 days |
*What is a Regular Expression?* A regular expression (RegEx) is a sequence of characters that forms a search pattern. It’s commonly used for: - Email and phone number validation - Searching for patterns in text - Data cleaning and extraction *Python’s re Module – Basics* import re *1. re.search() –* Looks for a match anywhere in the string text = My email is hello@example.com match = re.search(rw+@w+.w+, text) if match: print(Found:, match.group()) Output: Found: hello@example.com *2. re.findall() –* Returns all matches as a list text = Emails: one@mail.com and two@site.org emails = re.findall(rw+@w+.w+, text) print(emails) Output: ['one@mail.com', 'two@site.org'] *3. re.sub() – Replaces all matches* text = Hello 123, this is test 456 cleaned = re.sub(rd+, *, text) print(cleaned) Output: Hello *, this is test * *Common Patterns* - PatternDescription dAny digit (0-9) wAny alphanumeric character sAny whitespace (space/tab) .Any character (except newline) +One or more repetitions *Zero or more repetitions Start of string End of string *Mini Task: Extract Phone Numbers* text = Call me at 9876543210 or 9123456789 numbers = re.findall(rd10, text) print(Phone numbers found:, numbers) Output: ['9876543210', '9123456789'] *What You Practiced* : - Writing and using regular expressions - Using re.search(), re.findall(), and re.sub() - Cleaning and extracting patterns from text - Real-world data handling |
||


