程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python basics] regular expression

編輯:Python

Wish you all a happy new year in advance , This article should be the last article before the Spring Festival ~

A regular expression is a special sequence of characters , It can help you easily check whether a string matches a certain pattern ,Python Mainly through re Modules use regular expressions .

This article does not introduce the syntax of regular expressions itself , Just introduce Python Using regular expressions .

1 String search

1.1 re.search

Use re.search function , Scan the entire string and return the first successful match .

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

The meaning of each parameter is as follows :

pattern: Enter the regular expression string: String to match . flags: Sign a , Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .

If re.search Method matching succeeded , Then return a matching object ; Otherwise return to None. Let's illustrate with a simple example :

import re
result = re.search("bitpy", "www.bitpy.com")
print(result)

The output is as follows :

<re.Match object; span=(4, 9), match='bitpy'>

1.2 Extract matching results

In the previous section, we got the matching result object ( namely re.MatchObject object ), The corresponding information can be extracted according to the actual needs

Get matching interval

import re
result = re.search("bitpy", "www.bitpy.com")
print(result.span())

The output is as follows :

(4, 9)

besides , also start() Function returns the starting position of the match ;end() Function returns the position where the match ends .

Get matching content

If you need to extract key substrings , You can add parentheses to regular expressions , Then the matching result is passed group Function extraction .

import re
result = re.search(r"(.*)\.(.*)\.(.*)", "www.bitpy.com")
if result:
print(result.groups())
print(result.group(0))
print(result.group(1))
print(result.group(2))
print(result.group(3))

Note the second line , Precede quotation marks with letters r, Indicates that this string is a regular expression .

The output is as follows :

('www', 'bitpy', 'com')
www.bitpy.com
www
bitpy
com

2 string matching

re.match Try to match a pattern... From the beginning of the string , If the match is successful , return re.MatchObject object ; Otherwise return to none.

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

Illustrate with a simple example :

import re
result_1 = re.match(r"bitpy", "bitpy.com")
result_2 = re.match(r"(.*).com", "bitpy.com")
if result_1:
print(result_1.span())
if result_2:
print(result_2.groups())

Be careful ,re.match And back again re.MatchObject object , therefore , It can also be done through spangroup Wait for the function to obtain the corresponding information .

The output is as follows :

(0, 5)
('bitpy',)

3 Sign a flags Parameters

Regular expressions can contain optional flag modifiers to control the matching pattern . The modifier is specified as an optional flag . Multiple flags can be passed by biting OR(|) They specify . Such as re.I | re.M Is set to I and M sign :

Modifier

describe

re.I

Make match match case insensitive

re.L

Do localization identification (locale-aware) matching

re.M

Multi-line matching , influence ^ and $

re.S

send . Match all characters including line breaks

re.U

according to Unicode Character set parsing characters . This sign affects \w, \W, \b, \B.

re.X

This flag allows you to write regular expressions more easily by giving you a more flexible format .

Welcome to follow me 【Python Learning from actual combat 】, Learn a little every day , Make a little progress every day .

Long press attention 【Python Learning from actual combat 】

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved