search
HomeBackend DevelopmentPython TutorialPython regular expressions
Python regular expressionsNov 23, 2016 pm 02:07 PM
python

Regular expression is a special sequence of characters that can help you easily check whether a string matches a certain pattern. Python has added the re module since version 1.5, which provides Perl-style regular expression patterns. The

re module brings full regular expression capabilities to the Python language. The

compile function generates a regular expression object based on a pattern string and optional flag arguments. This object has a series of methods for regular expression matching and replacement. The

re module also provides functions identical to these methods, which take a pattern string as their first argument.

This chapter mainly introduces the commonly used regular expression processing functions in Python.

re.match function

re.match attempts to match a pattern from the beginning of a string.

Function syntax:

re.match(pattern, string, flags=0)

Function parameter description:

Parameter

Description

pattern matching regular expression Formula

string The string to be matched.

flags flags are used to control the matching method of regular expressions, such as whether it is case-sensitive, multi-line matching, etc.

The re.match method returns a matching object if the match is successful, otherwise it returns None.

We can use group(num) or groups() matching object function to get the matching expression.

Matching object method

Description

group(num=0) Matches the string of the entire expression, group() can enter multiple group numbers at once, in which case it will return a string containing A tuple of values ​​corresponding to those groups.

groups() Returns a tuple containing all group strings, from 1 to the group number contained in .

Example:

#!/usr/bin/python

import re

line = "Cats are smarter than dogs"

matchObj = re.match( r'(.* ) are (.*?) .*', line, re.M|re.I)

if matchObj:

print "matchObj.group() : ", matchObj.group()

print "matchObj .group(1) : ", matchObj.group(1)

print "matchObj.group(2) : ", matchObj.group(2)

else:

print "No match!!"

The execution result of the above example is as follows:

matchObj.group() : Cats are smarter than dogs

matchObj.group(1) : Cats

matchObj.group(2) : smarter

re.search method

re.match attempts to match a pattern from the beginning of the string.

Function syntax:

re.search(pattern, string, flags=0)

Function parameter description:

Parameter

Description

pattern matching regular expression Formula

string The string to be matched.

flags flags are used to control the matching method of regular expressions, such as whether it is case-sensitive, multi-line matching, etc.

The re.search method returns a matching object if the match is successful, otherwise it returns None.

We can use group(num) or groups() matching object function to get the matching expression.

Matching object method

Description

group(num=0) Matches the string of the entire expression, group() can enter multiple group numbers at once, in which case it will return a string containing A tuple of values ​​corresponding to those groups.

groups() Returns a tuple containing all group strings, from 1 to the group number contained in .

Example:

#!/usr/bin/python

import re

line = "Cats are smarter than dogs";

matchObj = re.match( r'(. *) are (.*?) .*', line, re.M|re.I)

if matchObj:

print "matchObj.group() : ", matchObj.group()

print " matchObj.group(1) : ", matchObj.group(1)

print "matchObj.group(2) : ", matchObj.group(2)

else:

print "No match!!"

The execution results of the above example are as follows:

matchObj.group() : Cats are smarter than dogs

matchObj.group(1) : Cats

matchObj.group(2) : smarter

The difference between re.match and re.search

re.match only matches the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and the function returns None; while re.search matches the entire string until a match is found.

Example:

#!/usr/bin/python

import re

line = "Cats are smarter than dogs";

matchObj = re.match( r'dogs' , line, re.M|re.I)

if matchObj:

print "match --> matchObj.group() : ", matchObj.group()

else:

print "No match!! "

matchObj = re.search( r'dogs', line, re.M|re.I)

if matchObj:

print "search --> matchObj.group() : ", matchObj. group()

else:

print "No match!!"

The results of the above example are as follows:

No match!!

search --> matchObj.group() : Dogs

Retrieval and replacement

Python’s re module provides re.sub for replacing matches in a string.

Syntax:

re.sub(pattern, repl, string, max=0)

The returned string is replaced with the leftmost non-repeating match of RE in the string. If the pattern is not found, the character will be returned unchanged.

The optional parameter count is the maximum number of substitutions after pattern matching; count must be a non-negative integer. The default value is 0 which replaces all matches.

Example:

#!/usr/bin/python

import re

phone = "2004-959-559 # This is Phone Number"

# Delete Python-style comments

num = re.sub(r'#.*$', "", phone)

print "Phone Num : ", num

# Remove anything other than digits

num = re.sub(r 'D', "", phone)

print "Phone Num: ", num

The above example execution results are as follows:

Phone Num : 2004-959-559

Phone Num : 2004959559

Regular expression modifiers - optional flags

Regular expressions can contain some optional flag modifiers to control the matched patterns. The modifier is specified as an optional flag. Multiple flags can be specified by bitwise OR(|) them. For example, re.I | re.M is set to I and M flags:

Modifier

Description

re.I Make the match case-insensitive

re.L Do localization recognition (locale -aware) Match

re.M Multi-line matching, affecting ^ and $

re.S Make. Match all characters including newlines

re.U Parse characters according to the Unicode character set. This flag affects w, W, b, B.

re.X This flag makes writing regular expressions easier to understand by giving you more flexible formatting.

Regular expression pattern

Pattern strings use special syntax to represent a regular expression:

letters and numbers represent themselves. Letters and numbers in a regular expression pattern match the same string.

Most letters and numbers have different meanings when preceded by a backslash.

Punctuation characters only match themselves if they are escaped, otherwise they represent a special meaning.

The backslash itself needs to be escaped with a backslash.

Since regular expressions usually contain backslashes, you'd better use raw strings to represent them. Pattern elements (such as r'/t', equivalent to '//t') match the corresponding special characters.

The following table lists the special elements in the regular expression pattern syntax. If you use a pattern and provide optional flags arguments, the meaning of some pattern elements will change.

Pattern

Description

^ Matches the beginning of the string

$ Matches the end of the string.

. Matches any character, except newline characters. When the re.DOTALL flag is specified, it can match any character including newline characters.

[...] Used to represent a group of characters, listed separately: [amk] Matches 'a', 'm' or 'k'

[^...] Characters not in []: [^ abc] matches characters except a, b, c.

re* Matches 0 or more expressions.

re+ Matches 1 or more expressions.

re? Matches 0 or 1 fragments defined by the previous regular expression, greedy mode

re{ n}

re{ n,} Exactly matches n previous expressions.

re{ n, m} Match n to m times the fragment defined by the previous regular expression, greedy way

a| b Match a or b

(re) G matches the expression in parentheses, also represents a The group

(?imx) regular expression contains three optional flags: i, m, or x. Only affects the area in brackets.

(?-imx) Regular expression to turn off the i, m, or x optional flags. Only affects the area in brackets.

(?: re) Like (...), but does not represent a group

(?imx: re) Use i, m, or x optional flags in parentheses

(?-imx: re) in Do not use i, m, or x optional flags in parentheses

(?#...) Comment.

(?= re) Forward positive delimiter. Succeeds if the contained regular expression, represented by ... , successfully matches the current position, otherwise it fails. But once the contained expression has been tried, the matching engine doesn't improve at all; the remainder of the pattern still has to try the right side of the delimiter.

(?! re) Forward negative delimiter. Contrary to the positive delimiter; succeeds when the contained expression cannot be matched at the current position of the string

(?> re) An independent pattern matched, eliminating backtracking.

w Matches alphanumeric characters

W Matches non-alphanumeric characters

s Matches any whitespace character, equivalent to [tnrf].

S Matches any non-empty character

d Matches any digit, equivalent to [0-9 ].

D Matches any non-number

A Matches the beginning of the string

Z Matches the end of the string. If there is a newline, only the ending string before the newline is matched. c

z Match the end of the string

G Match the position where the last match is completed.

b Matches a word boundary, which refers to the position between a word and a space. For example, 'erb' matches 'er' in "never" but not "er" in "verb".

B Matches non-word boundaries. 'erB' matches 'er' in "verb" but not in "never".

n, t, etc. Matches a newline character. Matches a tab character. etc.

1...9 matches the subexpression of the nth group.

10 Matches the subexpression of the nth group if it is matched. Otherwise it refers to the expression of the octal character code.

Regular Expression Example

Character Match

Example

Description

python Match "python".

Character Class

Instance

Description

[Pp]python Match" Python" or "python"

rub[ye] Matches "ruby" or "rube"

[aeiou] Matches any letter within the brackets

[0-9] Matches any number. Similar to [0123456789]

[a-z] Matches any lowercase letters

[A-Z] Matches any uppercase letters

[a-zA-Z0-9] Matches any letters and numbers

[^aeiou] Except for aeiou letters of All characters

[^0-9] Matches characters except numbers

Special character class

instance

Description

. Matches any single character except "n". To match any character including 'n', use a pattern like '[.n]'.

d Matches a numeric character. Equivalent to [0-9].

D Matches a non-numeric character. Equivalent to [^0-9].

s Matches any whitespace characters, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv].

S matches any non-whitespace character. Equivalent to [^ fnrtv].

w Matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'.

W matches any non-word character. Equivalent to '[^A-Za-z0-9_]'.


Statement
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
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version