Software Testing|In-depth understanding of the difference between re.search() and re.findall() in Python

Insert image description here

Preface

In Python, regular expressions are a powerful tool for finding, matching, and manipulating patterns in text. The re module provides many functions to process regular expressions, of which re.search()and re.findall()are two commonly used functions for finding matching patterns in strings. This article will provide an in-depth introduction to the usage of these two functions, as well as detailed usage examples.

re.search() function

re.search()The function is used to find the first matching substring in a string and returns a matching object. If a match is found, relevant information can be obtained through the methods and properties of the matching object.

import re

pattern = r'apple'
text = "I have an apple and a banana."

# 在文本中查找第一个匹配的子串
match = re.search(pattern, text)

if match:
    print("Found:", match.group())  # 获取匹配的子串
    print("Start:", match.start())  # 获取匹配的起始位置
    print("End:", match.end())      # 获取匹配的结束位置
else:
    print("No match found.")

re.findall() function

re.findall()The function is used to find all matching substrings in a string and returns a list containing all matching results.

import re

pattern = r'\d+'  # 匹配一个或多个数字
text = "I have 3 apples and 5 bananas. Total 8 fruits."

# 查找所有匹配的子串
matches = re.findall(pattern, text)

if matches:
    print("Matches:", matches)  # 获取所有匹配的子串列表
else:
    print("No matches found.")

Usage example

  1. re.search()Find a date using
import re

pattern = r'\d{2}-\d{2}-\d{4}'  # 匹配日期格式:dd-mm-yyyy
text = "Today's date is 31-08-2023."

match = re.search(pattern, text)

if match:
    print("Date found:", match.group())
else:
    print("No date found.")

  1. re.findall()Find all links using
import re

pattern = r'https?://\S+'  # 匹配HTTP或HTTPS链接
text = "Here are some links: https://www.example.com and http://google.com"

links = re.findall(pattern, text)

if links:
    print("Links found:", links)
else:
    print("No links found.")
  1. re.findall()Find an email address using
import re

pattern = r'\w+@\w+\.\w+'  # 匹配基本电子邮件地址
text = "Contact us at [email protected] or [email protected]"

emails = re.findall(pattern, text)

if emails:
    print("Email addresses found:", emails)
else:
    print("No email addresses found.")

Summarize

re.search()is used to find the first matching substring, while re.findall() is used to find all matching substrings. By defining appropriate rules in regular expression patterns, we can effectively find and process various patterns in text. These two functions are important tools for handling text matching and searching, and are very useful in text processing and data extraction.

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132606170