GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python re.search - Mastering Regular Expression Searching Python re.search Functionlast modified April 20, 2025 Introduction to re.search
The This function returns a match object if found, or None if no match exists. It's one of the most commonly used functions in Python's regex module.
The search stops at the first match found in the string. For all matches,
use Basic Syntax
The syntax for re.search(pattern, string, flags=0)
The Basic Pattern SearchLet's start with a simple example of searching for a pattern in text. basic_search.py
#!/usr/bin/python
import re
text = "The quick brown fox jumps over the lazy dog"
match = re.search(r'fox', text)
if match:
print(f"Found '{match.group()}' at position {match.start()}")
else:
print("Pattern not found")
This example demonstrates the basic usage of match = re.search(r'fox', text)
The raw string ( if match:
print(f"Found '{match.group()}' at position {match.start()}")
If a match is found, we print the matched text and its starting position.
The Using Flags with re.searchFlags modify how the pattern matching behaves. Here's a case-insensitive search. flags_example.py
#!/usr/bin/python
import re
text = "Python is FUN!"
match = re.search(r'fun', text, re.IGNORECASE)
if match:
print(f"Found '{match.group()}' ignoring case")
else:
print("Pattern not found")
The Searching for DigitsRegular expressions can match complex patterns like sequences of digits. digits_example.py
#!/usr/bin/python
import re
text = "Order 12345 was placed on 2023-05-20"
match = re.search(r'\d+', text)
if match:
print(f"Found number: {match.group()}")
else:
print("No numbers found")
The pattern Extracting Email Addresses
We can use email_example.py
#!/usr/bin/python import re text = "Contact us at [email protected] or [email protected]" match = re.search(r'[\w\.-]+@[\w\.-]+', text) if match: print(f"Found email: {match.group()}") else: print("No email found")
This pattern matches common email formats. The character class Using Groups in SearchParentheses create capturing groups that can be extracted separately. groups_example.py
#!/usr/bin/python
import re
text = "Date: 2023-12-25"
match = re.search(r'Date: (\d{4})-(\d{2})-(\d{2})', text)
if match:
print(f"Year: {match.group(1)}")
print(f"Month: {match.group(2)}")
print(f"Day: {match.group(3)}")
else:
print("Date pattern not found")
This extracts date components into separate groups. Group 0 is the full match, while groups 1-3 contain the captured date parts. Searching with Word Boundaries
Word boundaries ( word_boundaries.py
#!/usr/bin/python
import re
text = "The cat in the hat"
match = re.search(r'\bcat\b', text)
if match:
print("Found exact word 'cat'")
else:
print("Word 'cat' not found")
The Searching in Multiline Text
The multiline_example.py
#!/usr/bin/python
import re
text = """First line
Second line
Third line"""
match = re.search(r'^Second.*$', text, re.MULTILINE)
if match:
print(f"Found line: {match.group()}")
else:
print("Line not found")
With Best Practices
When using
Performance Considerations
For single searches, Complex patterns with excessive backtracking can be slow. Test performance with realistic input data when working with complex regexes. SourcePython re.search() documentation
This tutorial covered the essential aspects of Python's AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |