| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
Welcome to codeswithpankaj.com! In this tutorial, we will explore how to work with regular expressions (RegEx) in Python using the re module. We'll cover the basics of RegEx, how to use them in Python, and provide detailed examples to illustrate their application.
Regular expressions (RegEx) are sequences of characters that form search patterns. They are used to match strings of text, such as particular characters, words, or patterns of characters.
The re module in Python provides functions to work with regular expressions.
import re
# Find all occurrences of 'cat' in the string
pattern = r'cat'
text = 'The cat sat on the cat mat.'
matches = re.findall(pattern, text)
print(matches)Finds all matches of a pattern in a string.
import re
pattern = r'\d+' # Matches one or more digits
text = 'There are 123 cats and 456 dogs.'
matches = re.findall(pattern, text)
print(matches) # Output: ['123', '456']Searches for the first match of a pattern in a string.
import re
pattern = r'\d+' # Matches one or more digits
text = 'There are 123 cats and 456 dogs.'
match = re.search(pattern, text)
if match:
print(match.group()) # Output: 123Checks for a match only at the beginning of the string.
import re
pattern = r'\d+' # Matches one or more digits
text = '123 cats and 456 dogs.'
match = re.match(pattern, text)
if match:
print(match.group()) # Output: 123Replaces matches of a pattern with a string.
import re
pattern = r'cat'
text = 'The cat sat on the cat mat.'
new_text = re.sub(pattern, 'dog', text)
print(new_text) # Output: The dog sat on the dog mat.Splits a string by the occurrences of a pattern.
import re
pattern = r'\d+'
text = 'one1two2three3four4'
split_text = re.split(pattern, text)
print(split_text) # Output: ['one', 'two', 'three', 'four', '']Regular expressions can be compiled into pattern objects, which can be used to perform matches more efficiently.
import re
pattern = re.compile(r'\d+')
text = 'There are 123 cats and 456 dogs.'
matches = pattern.findall(text)
print(matches) # Output: ['123', '456']
match = pattern.search(text)
if match:
print(match.group()) # Output: 123import re
def validate_email(email):
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
return re.match(pattern, email) is not None
email = 'test@example.com'
print(validate_email(email)) # Output: Trueimport re
text = 'Contact us at 123-456-7890 or 987-654-3210.'
pattern = r'\d{3}-\d{3}-\d{4}'
phone_numbers = re.findall(pattern, text)
print(phone_numbers) # Output: ['123-456-7890', '987-654-3210']import re
text = 'The event is on 2024-08-05. Another event is on 2024-09-15.'
pattern = r'\d{4}-\d{2}-\d{2}'
new_text = re.sub(pattern, 'YYYY-MM-DD', text)
print(new_text) # Output: The event is on YYYY-MM-DD. Another event is on YYYY-MM-DD.import re
text = 'apple;banana,orange|grape'
pattern = r'[;,|]'
split_text = re.split(pattern, text)
print(split_text) # Output: ['apple', 'banana', 'orange', 'grape']In this tutorial, we explored how to work with regular expressions in Python using the re module. We covered the basics of RegEx, how to use various functions in the re module, compiling regular expressions, and provided practical examples to illustrate their application. Regular expressions are powerful tools for searching, manipulating, and validating strings in Python.
For more tutorials and in-depth explanations, visit codeswithpankaj.com!
This tutorial provides a comprehensive overview of working with regular expressions in Python, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!
| Back | FazBrowse Home | New Git URL |