Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Re module of Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the Re module of Python. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Summary of basic Grammar

1.1. Match a single character

A. D D w W s S [...] [^...]

Match a single character (.)

Rule: match any character except newline

In [24]: re.findall ("f.o", "foo is not fao") Out [24]: ['foo',' fao']

Match any (non) numeric character (d D)

D [0-9] D [^ 0-9]

Match any (non) ordinary character (w W)

W ordinary characters include [_ 0-9A-Za-z] as well as Chinese characters

W non-ordinary character

Match any (non) empty character (s S)

S matches any empty character []

S matches any non-empty character

Matching character set ([...])

[Amurz] [aMuz] [0-9] [_ 123a-z]

Match character set ([^.])

Rule: character set is negative or negative, any character except the one listed

[^ abc]-- > any character except abc

1.2, match multiple characters

* match 0 or more times

+ match one or more times

? Match 0 times or 1 time

{m} match m times

{mdirection n} match m times to n times any time in the interval

1.3, matching position

^ match start position

$match end position

A match start position

Z matching end position

Match the boundary position of the word (usually used to match the first letter in uppercase)

B matching non-word boundary problem

1.4. Escape

There is a class of special characters in regular expressions that need to be transferred. You only need to add a representation transfer between the special characters.

. * +? ^ $[] {} () |

1.5, subgroup

Use () to establish an internal grouping of regular expressions, and the subgroups are part of the regular expression and can be seen as an internal whole.

In [61]: re.search (r "(https | http | ftp): / / com | cn. (com | cn)", "https://www.baidu.com").group(0) Out [61]: 'https://www.baidu.com' In [62]: re.search (r" (https | http | ftp): / / wicked. (com | cn) "," https://www.baidu.com").group(1) Out [62]:' https'

1.6. Greedy mode and non-greedy mode

The repeated matching of regular expressions always matches as much backward as possible. The greed mode includes: * +? {mrecom n}.

Non-greedy mode: as few matches as possible, greedy mode is converted to non-greedy mode: *? +? {mMagne}?

In: re.findall (r "ab+?", "abbbbbbbb") Out [106]: [ab'] In [107]: re.findall (r "ab??", "abbbbbbbb") Out [107]: ['a']

2. Re module

Next, the parameters in all my functions are explained as follows:

Pattern: regular expression

String: target string

Pos: intercepts the starting position of the target string

Endpose: intercepts the end position of the target string

Flags: feature Fla

ReplaceStr: replaced string

Max: replace at most several places (replace all by default)

As we can see from the above picture, there is a certain relationship among re module, regex object and match object in Python.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

The compile method of the re module returns a regex object

The finditer (), fullmatch (), match (), search () and other methods of the re module and regex object return a match object

They have their own properties and methods respectively.

2.1 、 compile

Regex = re.compile (pattern, flags = 0) # generate regular expression objects

2.2 、 findall

Re.findall (pattern,string,pos,endpose) # matches all eligible content from the target string

2.3 、 split

Re.split (pattern,string,flags) # splits the target string according to the regular expression In [79]: re.split (rattlespace, "Hello World") Out [79]: ['Hello',' World']

2.4 、 sub

Re.sub (pattern,replaceStr,string,max,flags) In [80]: re.sub (rushing slots, "# #", "hello world") Out [80]: 'hello##world'

2.5 、 subn

Re.subn (pattern,replaceStr,string,max,flags) # function is the same as sub, but the return value returns the replaced string and replaces several places of In [80]: re.sub (ringing slots, "# #", "hello world") Out [80]: ('hello##world',1)

2.6 、 finditer

Re.finditer (pattern,string) # uses regular expressions to match the target string and returns a match object The match object can only get the value In [87]: it = re.finditer (rattledcustomers, "2014nianshiqiqngduo 08aoyun 512dizhen") In [88]: for i in it:...: print (I)....: In [93]: it = re.finditer (rusted clients, "2014nianshiqiqngduo 08aoyun 512dizhen") In [94]: for i in it:....: print (i.group ()): 2014 08 512.

2.7 、 fullmatch

Fullmatch (pattern,string,flags) # exactly matches the target string, equivalent to adding ^ and $

2.8 、 match

Re.match (pattern,string,flags) # matches the position at the beginning of the target string

2.9 、 search

Re.search (pattern,string,flags) # regular expression matches the target string, only at *

3. Some exercises

3.1. Match words with uppercase initials

Import re f = open ('test.txt') pattern= r' [Amurz] [a-zA-Z] * Squires' # pattern= r' [Amurz] S'l = [] for i in f: l + = re.findall (pattern,i) print (L)

The test.txt document is as follows:

Hello World-12.6 Nihao 123How are you-121.24 asdk 34%, accounting for 1 2003-2005 asdk%

3.2, matching numbers (positive, negative, decimal, percentage, fraction)

Import re pattern = "-? d + ((/? d +) | (.)? d +) | ((%)?)" F = open ('test.txt') l = [] for line in f: l + = re.finditer (pattern,line) for i in l: print (i.group ()) these are all the contents of the article "how to use Python's Re Module". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report