2024-05-21 16:31:17 +00:00
## Regular Expressions in Python
2024-06-26 09:01:54 +00:00
Regular expressions (regex) are a powerful tool for pattern matching and text manipulation.
2024-05-21 16:31:17 +00:00
Python's re module provides comprehensive support for regular expressions, enabling efficient text processing and validation.
2024-06-22 18:34:05 +00:00
Regular expressions (regex) are a versitile tool for matching patterns in strings. In Python, the `re` module provides support for working with regular expressions.
2024-05-21 16:31:17 +00:00
## 1. Introduction to Regular Expressions
2024-06-26 09:01:54 +00:00
A regular expression is a sequence of characters defining a search pattern. Common use cases include validating input, searching within text, and extracting
2024-05-21 16:31:17 +00:00
specific patterns.
## 2. Basic Syntax
Literal Characters: Match exact characters (e.g., abc matches "abc").
2024-06-26 09:01:54 +00:00
Metacharacters: Special characters like ., \*, ?, +, ^, $, [ ], and | used to build patterns.
2024-05-21 16:31:17 +00:00
**Common Metacharacters:**
2024-06-26 09:01:54 +00:00
- .: Any character except newline.
- ^: Start of the string.
- $: End of the string.
2024-06-26 19:42:55 +00:00
- *: 0 or more repetitions.
2024-06-26 09:01:54 +00:00
- +: 1 or more repetitions.
- ?: 0 or 1 repetition.
- []: Any one character inside brackets (e.g., [a-z]).
- |: Either the pattern before or after.
- \ : Used to drop the special meaning of character following it
- {} : Indicate the number of occurrences of a preceding regex to match.
- () : Enclose a group of Regex
2024-06-22 18:34:05 +00:00
Examples:
2024-06-26 09:01:54 +00:00
2024-06-22 18:34:05 +00:00
1. `.`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
import re
pattern = r'c.t'
text = 'cat cot cut cit'
matches = re.findall(pattern, text)
print(matches) # Output: ['cat', 'cot', 'cut', 'cit']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
2. `^`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'^Hello'
text = 'Hello, world!'
match = re.search(pattern, text)
print(match.group() if match else 'No match') # Output: 'Hello'
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
3. `$`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'world!$'
text = 'Hello, world!'
match = re.search(pattern, text)
print(match.group() if match else 'No match') # Output: 'world!'
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
4. `*`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'ab*'
text = 'a ab abb abbb'
matches = re.findall(pattern, text)
print(matches) # Output: ['a', 'ab', 'abb', 'abbb']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
5. `+`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'ab+'
text = 'a ab abb abbb'
matches = re.findall(pattern, text)
print(matches) # Output: ['ab', 'abb', 'abbb']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
6. `?`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'ab?'
text = 'a ab abb abbb'
matches = re.findall(pattern, text)
print(matches) # Output: ['a', 'ab', 'ab', 'ab']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
7. `[]`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'[aeiou]'
text = 'hello world'
matches = re.findall(pattern, text)
print(matches) # Output: ['e', 'o', 'o']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
8. `|`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'cat|dog'
text = 'I have a cat and a dog.'
matches = re.findall(pattern, text)
print(matches) # Output: ['cat', 'dog']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
9. `\` `
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'\$100'
text = 'The price is $100.'
match = re.search(pattern, text)
print(match.group() if match else 'No match') # Output: '$100'
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
10. `{}`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'\d{3}'
text = 'My number is 123456'
matches = re.findall(pattern, text)
print(matches) # Output: ['123', '456']
2024-06-26 09:01:54 +00:00
```
2024-06-22 18:34:05 +00:00
11. `()`
2024-06-26 09:01:54 +00:00
```bash
2024-06-22 18:34:05 +00:00
pattern = r'(cat|dog)'
text = 'I have a cat and a dog.'
matches = re.findall(pattern, text)
print(matches) # Output: ['cat', 'dog']
```
2024-05-21 16:31:17 +00:00
## 3. Using the re Module
**Key functions in the re module:**
2024-06-26 09:01:54 +00:00
- re.match(): Checks for a match at the beginning of the string.
- re.search(): Searches for a match anywhere in the string.
- re.findall(): Returns a list of all matches.
- re.sub(): Replaces matches with a specified string.
- re.split(): Returns a list where the string has been split at each match.
- re.escape(): Escapes special character
Examples:
2024-05-21 16:31:17 +00:00
```bash
import re
# Match at the beginning
print(re.match(r'\d+', '123abc').group()) # Output: 123
# Search anywhere
print(re.search(r'\d+', 'abc123').group()) # Output: 123
# Find all matches
print(re.findall(r'\d+', 'abc123def456')) # Output: ['123', '456']
# Substitute matches
print(re.sub(r'\d+', '#', 'abc123def456')) # Output: abc#def#
2024-06-22 18:34:05 +00:00
2024-06-26 09:01:54 +00:00
#Return a list where it get matched
2024-06-22 18:34:05 +00:00
print(re.split("\s", txt)) #['The', 'Donkey', 'in', 'the','Town']
# Escape special character
print(re.escape("We are good to go")) #We \ are\ good\ to\ go
2024-05-21 16:31:17 +00:00
```
## 4. Compiling Regular Expressions
2024-06-26 09:01:54 +00:00
2024-05-21 16:31:17 +00:00
Compiling regular expressions improves performance for repeated use.
Example:
2024-06-26 09:01:54 +00:00
2024-05-21 16:31:17 +00:00
```bash
import re
pattern = re.compile(r'\d+')
print(pattern.match('123abc').group()) # Output: 123
print(pattern.search('abc123').group()) # Output: 123
print(pattern.findall('abc123def456')) # Output: ['123', '456']
2024-06-22 18:34:05 +00:00
2024-05-21 16:31:17 +00:00
```
## 5. Groups and Capturing
2024-06-26 09:01:54 +00:00
2024-05-21 16:31:17 +00:00
Parentheses () group and capture parts of the match.
Example:
2024-06-26 09:01:54 +00:00
2024-05-21 16:31:17 +00:00
```bash
import re
match = re.match(r'(\d{3})-(\d{2})-(\d{4})', '123-45-6789')
if match:
print(match.group()) # Output: 123-45-6789
print(match.group(1)) # Output: 123
print(match.group(2)) # Output: 45
print(match.group(3)) # Output: 6789
```
## 6. Special Sequences
2024-06-26 09:01:54 +00:00
2024-05-21 16:31:17 +00:00
Special sequences are shortcuts for common patterns:
2024-06-26 09:01:54 +00:00
- \A:Returns a match if the specified characters are at the beginning of the string.
- \b:Returns a match where the specified characters are at the beginning or at the end of a word.
- \B:Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word.
- \d: Any digit.
- \D: Any non-digit.
- \w: Any alphanumeric character.
- \W: Any non-alphanumeric character.
- \s: Any whitespace character.
- \S: Any non-whitespace character.
- \Z:Returns a match if the specified characters are at the end of the string.
2024-06-22 18:34:05 +00:00
2024-05-21 16:31:17 +00:00
Example:
2024-06-26 09:01:54 +00:00
2024-05-21 16:31:17 +00:00
```bash
import re
print(re.search(r'\w+@\w+\.\w+', 'Contact: support@example.com').group()) # Output: support@example.com
```
2024-06-26 09:01:54 +00:00
2024-06-22 18:34:05 +00:00
## 7.Sets
2024-06-26 09:01:54 +00:00
2024-06-22 18:34:05 +00:00
A set is a set of characters inside a pair of square brackets [] with a special meaning:
2024-06-26 09:01:54 +00:00
- [arn] : Returns a match where one of the specified characters (a, r, or n) is present.
- [a-n] : Returns a match for any lower case character, alphabetically between a and n.
- [^arn] : Returns a match for any character EXCEPT a, r, and n.
- [0123] : Returns a match where any of the specified digits (0, 1, 2, or 3) are present.
- [0-9] : Returns a match for any digit between 0 and 9.
- [0-5][0-9] : Returns a match for any two-digit numbers from 00 and 59.
- [a-zA-Z] : Returns a match for any character alphabetically between a and z, lower case OR upper case.
- [+] : In sets, +, \*, ., |, (), $,{} has no special meaning
- [+] means: return a match for any + character in the string.
2024-05-21 16:31:17 +00:00
## Summary
2024-06-26 09:01:54 +00:00
2024-06-22 18:34:05 +00:00
Regular expressions (regex) are a powerful tool for text processing in Python, offering a flexible way to match, search, and manipulate text patterns. The re module provides a comprehensive set of functions and metacharacters to tackle complex text processing tasks.
With regex, you can:
2024-06-26 09:01:54 +00:00
1.Match patterns: Use metacharacters like ., \*, ?, and {} to match specific patterns in text.
2024-06-22 18:34:05 +00:00
2.Search text: Employ functions like re.search() and re.match() to find occurrences of patterns in text.
3.Manipulate text: Utilize functions like re.sub() to replace patterns with new text.