How to use the re module for regular expression matching in Python 3.x

WBOY
Release: 2023-07-30 09:00:26
Original
1608 people have browsed it

How to use the re module for regular expression matching in Python 3.x

Regular expression is a powerful text processing tool that can be used to match, find and replace specific patterns in strings . In Python, we can use the re module to perform regular expression matching operations.

First, we need to import the re module:

import re
Copy after login

Next, we can use the functions provided by the re module to match regular expressions.

  1. re.match() function

re.match() function can start matching from the beginning of the string. If the match is successful, it returns a matching object. ; Otherwise, None is returned.

import re

pattern = r"hello"
string = "hello world"

result = re.match(pattern, string)

if result:
    print("匹配成功")
else:
    print("匹配失败")
Copy after login

The output result is:

匹配成功
Copy after login
Copy after login
  1. re.search() function

re.search() function will search the entire character in the string string until the first match is found. If the match is successful, a matching object is returned; otherwise None is returned.

import re

pattern = r"world"
string = "hello world"

result = re.search(pattern, string)

if result:
    print("匹配成功")
else:
    print("匹配失败")
Copy after login

The output result is:

匹配成功
Copy after login
Copy after login
  1. re.findall() function

The re.findall() function will search the entire character in the string string and returns a list of all matching items.

import re

pattern = r"o"
string = "hello world"

result = re.findall(pattern, string)

print(result)
Copy after login

The output result is:

['o', 'o', 'o']
Copy after login
  1. re.split() function

re.split() function can match the regular expression to split a string and return a list of split strings.

import re

pattern = r"s+"
string = "hello        world"

result = re.split(pattern, string)

print(result)
Copy after login

The output result is:

['hello', 'world']
Copy after login
  1. re.sub() function

re.sub() function is used to find and Replace matches.

import re

pattern = r"world"
string = "hello world"

result = re.sub(pattern, "universe", string)

print(result)
Copy after login

The output result is:

hello universe
Copy after login

The above are some common functions and examples of using the re module for regular expression matching. Through the flexible use of regular expressions, we can easily process and analyze text. I hope this article will help you learn and use regular expressions in Python.

The above is the detailed content of How to use the re module for regular expression matching in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!