杰瑞科技汇

Python search match如何实现高效匹配?

Of course! The term "Python search match" can mean several different things depending on your goal. Let's break it down into the most common scenarios, from simple to advanced.

Python search match如何实现高效匹配?-图1
(图片来源网络,侵删)

Here are the main interpretations:

  1. Simple Substring Search: Checking if a string contains another string.
  2. Pattern Matching (Regex): Finding complex patterns (like phone numbers, emails) within text.
  3. Searching in Lists: Finding an item in a list of data.
  4. Searching in Dictionaries/Objects: Finding a value by its key or searching through values.
  5. Searching Files: Finding text within files on your computer.

Let's go through each one with clear examples.


Simple Substring Search (The in keyword)

This is the most basic way to check if one string exists inside another. It's fast, readable, and perfect for simple checks.

Key Methods:

  • substring in string: Returns True or False.
  • string.find(substring): Returns the lowest index where the substring is found. Returns -1 if not found.
  • string.index(substring): Returns the lowest index where the substring is found. Raises a ValueError if not found (use with a try...except block).

Example:

main_string = "Hello, welcome to the world of Python programming!"
substring_to_find = "Python"
# Using the 'in' keyword (most common for a simple True/False check)
if substring_to_find in main_string:
    print(f"✅ Found '{substring_to_find}' using 'in'")
else:
    print(f"❌ Did not find '{substring_to_find}' using 'in'")
# Using the .find() method
position = main_string.find(substring_to_find)
if position != -1:
    print(f"✅ Found '{substring_to_find}' at index {position} using '.find()'")
else:
    print(f"❌ Did not find '{substring_to_find}' using '.find()'")
# Using the .index() method (be careful with this one)
try:
    position = main_string.index(substring_to_find)
    print(f"✅ Found '{substring_to_find}' at index {position} using '.index()'")
except ValueError:
    print(f"❌ Did not find '{substring_to_find}' using '.index()'")

Output:

Python search match如何实现高效匹配?-图2
(图片来源网络,侵删)
✅ Found 'Python' using 'in'
✅ Found 'Python' at index 25 using '.find()'
✅ Found 'Python' at index 25 using '.index()'

Pattern Matching with Regular Expressions (Regex)

When you need to search for complex patterns (e.g., an email address, a specific date format, or a word that starts with 'a' and ends with 'e'), Regular Expressions (regex) are the powerful tool for the job.

Python's re module is your best friend here.

Key Methods:

  • re.search(pattern, string): Scans through the string, looking for the first location where the pattern matches. Returns a match object if found, otherwise None.
  • re.findall(pattern, string): Finds all non-overlapping matches of the pattern in the string and returns them as a list of strings.
  • re.match(pattern, string): Checks for a match only at the beginning of the string.

Example:

Let's find all email addresses in a block of text.

import re
text = """
Contact us at support@example.com or sales@my-company.co.uk.
For billing, please use billing@example.com.
This is not an email: invalid-email@.com.
"""
# The regex pattern for a basic email address
# \S+ matches one or more non-whitespace characters
# @ matches the literal '@'
# \S+ matches the domain part
email_pattern = r'\S+@\S+'
# re.findall() returns a list of all matches
found_emails = re.findall(email_pattern, text)
print("Found emails:")
for email in found_emails:
    print(f"- {email}")
# Using re.search to find just the first one
first_match = re.search(email_pattern, text)
if first_match:
    print(f"\nFirst email found: {first_match.group()}")
    # .start() and .end() give the position of the match
    print(f"  Found from index {first_match.start()} to {first_match.end()}")

Output:

Found emails:
- support@example.com
- sales@my-company.co.uk
- billing@example.com
- invalid-email@.com
First email found: support@example.com
  Found from index 15 to 33

Searching in Lists

To find an item in a list, you typically want to know its position or simply if it exists.

Key Methods:

  • item in list: Checks for existence (returns True/False).
  • list.index(item): Returns the index of the first occurrence of the item. Raises a ValueError if not found.
  • List Comprehension [x for x in list if condition]: Creates a new list with items that match a condition.

Example:

fruits = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
# Check for existence
if "cherry" in fruits:
    print("✅ Cherry is in the list.")
# Find the index
try:
    index_of_kiwi = fruits.index("kiwi")
    print(f"Kiwi is at index: {index_of_kiwi}")
except ValueError:
    print("Kiwi is not in the list.")
# Find all items that start with 'a' (case-sensitive)
a_fruits = [fruit for fruit in fruits if fruit.startswith("a")]
print(f"Fruits starting with 'a': {a_fruits}")
# Find all items that contain 'an'
an_fruits = [fruit for fruit in fruits if "an" in fruit]
print(f"Fruits containing 'an': {an_fruits}")

Output:

✅ Cherry is in the list.
Kiwi is at index: 4
Fruits starting with 'a': ['apple']
Fruits containing 'an': ['banana', 'mango']

Searching in Dictionaries / Objects

Dictionaries store key-value pairs. You search by key. If you need to search through the values, you have to iterate.

Key Methods:

  • key in dictionary: Checks if a key exists (very fast).
  • dictionary.get(key): Returns the value for a key if it exists, otherwise returns None (or a default you specify).
  • Looping through values: [value for value in my_dict.values() if condition]

Example:

student_grades = {
    "Alice": 92,
    "Bob": 88,
    "Charlie": 95,
    "Diana": 91
}
# Search by key (very efficient)
if "Bob" in student_grades:
    print(f"Bob's grade is: {student_grades['Bob']}")
# Use .get() for a safe access (avoids KeyError)
diana_grade = student_grades.get("Diana")
print(f"Diana's grade is: {diana_grade}")
# Get a grade for a student who might not exist
eve_grade = student_grades.get("Eve", "Student not found")
print(f"Eve's grade is: {eve_grade}")
# Search through values to find all students with a grade > 90
top_students = [student for student, grade in student_grades.items() if grade > 90]
print(f"Top students: {top_students}")

Output:

Bob's grade is: 88
Diana's grade is: 91
Eve's grade is: Student not found
Top students: ['Alice', 'Charlie', 'Diana']

Searching Files on Your System

If you want to find all files in a directory (and its subdirectories) that match a certain name or pattern, the glob module is perfect.

Key Methods:

  • glob.glob(path_pattern): Returns a list of pathnames that match the pattern. is a wildcard for any characters, is for recursive directories.
  • glob.iglob(path_pattern): An "iterator" version that returns one match at a time, which is more memory-efficient for large numbers of files.

Example:

Find all Python files (.py) in the current directory and all subdirectories.

import glob
import os
# Create some dummy files and directories for the example
if not os.path.exists("project/src"):
    os.makedirs("project/src")
with open("main.py", "w") as f: f.write("")
with open("project/utils.py", "w") as f: f.write("")
with open("project/src/helper.py", "w") as f: f.write("")
# Use '**' to search recursively
# The '**/' means "match any directory or subdirectory"
python_files = glob.glob("**/*.py", recursive=True)
print("Found Python files:")
for file_path in python_files:
    print(f"- {file_path}")
# Clean up the dummy files
os.remove("main.py")
os.remove("project/utils.py")
os.remove("project/src/helper.py")
os.rmdir("project/src")
os.rmdir("project")

Output:

Found Python files:
- main.py
- project/utils.py
- project/src/helper.py

Summary: Which one should I use?

Goal Best Tool / Method
Check if string A is in string B if A in B:
Find the position of a substring my_string.find(A) or my_string.index(A)
Find complex patterns (emails, etc.) re.search() or re.findall() from the re module
Check if an item is in a list if item in my_list:
Find the index of an item in a list my_list.index(item)
Check if a key exists in a dict if key in my_dict:
Get a value from a dict safely my_dict.get(key)
Find files matching a name pattern glob.glob("**/pattern", recursive=True)
分享:
扫描分享到社交APP
上一篇
下一篇