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

Python regular expression

編輯:Python

Author:AXYZdong Automation Engineering Male A little bit of thinking , Have a little idea , A little bit rational ! Set a small goal , Try to be a habit ! Meet a better self in the most beautiful years !

Regular expressions , abbreviation regex , Is a description of the text .

When writing programs or web pages that handle strings , There is often a need to find strings that meet certain complex rules , Regular expressions are tools for describing these rules , let me put it another way Regular expressions are a tool , It defines the matching pattern of strings ( How to check whether a string has a part that matches a pattern, or extract or replace the part that matches a pattern from a string ).

for example ,\d Is a regular expression , Represents a digit character , Any one of them 0 To 9 The number of .

Common regular expressions

Symbol

explain

Example

explain

.

Match any character

b.t

Can match bat / but / b#t / b1t etc.

\w

Match the letter / Numbers / Underline

b\wt

Can match bat / b1t / b_t etc. But can't match b#t

\s

Matching blank character ( Include \r、\n、\t etc. )

love\syou

Can match love you

\d

Match the Numbers

\d\d

Can match 01 / 23 / 99 etc.

\b

Match the boundaries of words

\bThe\b

^

Matches the beginning of the string

^The

Can match The Starting string

$

Matches the end of the string

.exe$

Can match .exe a null-terminated string

\W

Match nonletter / Numbers / Underline

b\Wt

Can match b#t / [email protected] etc. But can't match but / b1t / b_t etc.

\S

Match non white space characters

love\Syou

Can match love#you etc. But can't match love you

\D

Match non numeric

\d\D

Can match 9a / 3# / 0F etc.

\B

Match non word boundaries

\Bio\B

[]

Matches any single character from the character set

aeiou

Can match any vowel character

^

Matches any single character that is not in the character set

^aeiou

Can match any non vowel character

matching 0 Times or times

\w*

matching 1 Times or times

\w+

?

matching 0 Time or 1 Time

\w?

{N}

matching N Time

\w{3}

{M,}

Match at least M Time

\w{3,}

{M,N}

Match at least M Times at most N Time

\w{3,6}

|

Branch

foo|bar

Can match foo perhaps bar

(?#)

notes

(exp)

matching exp And capture it into an automatically named group

(?<name>exp)

matching exp And capture a file named name In the group of

(?:exp)

matching exp But do not capture matching text

(?=exp)

matching exp Front position

\b\w+(?=ing)

Can match I'm dancing Medium danc

(?<=exp)

matching exp The back position

(?<=\bdanc)\w+\b

Can match I love dancing and reading The first of ing

(?!exp)

After matching, it's not exp The location of

(?<!exp)

Not before match exp The location of

*?

Repeat any number of times , But repeat as little as possible

a.b a.?b

Apply regular expressions to aabab, The former will match the entire string aabab, The latter will match aab and ab Two strings

+?

repeat 1 Times or times , But repeat as little as possible

??

repeat 0 Time or 1 Time , But repeat as little as possible

{M,N}?

repeat M To N Time , But repeat as little as possible

{M,}?

repeat M More than once , But repeat as little as possible

Steps to use regular expressions

  1. import re Import regular expression module
  2. use re.compile() Function to create a Regex object ( Remember to use the original string )
  3. towards Regex Object's search() Method to pass in the string you want to find . It returns a March object .
  4. call March Object's group() Method , Returns the string that actually matches the text .

Regular expression test site : http://regexpal.com/

Use regular expressions to find phone numbers :

>>> import re
>>> phoneNumRegex = re.compile (r'\d\d\d-\d\d\d-\d\d\d\d') # establish Regex object ( Use original string , Simplify writing )
>>> mo = phoneNumRegex.search ('My number is 515-345-7890') # utilize search Method to pass in the string you want to find
>>> print('Phone number found :' + mo.group ()) # call March Object's group() Method , Returns the string that actually matches the text
Phone number found :515-345-7890
  • towards re.compile() Pass in a string value , This string represents a regular expression , It will return a Regex Pattern object .
  • Regex Object's search() Method to find the incoming string , Find all matches of regular expressions . Can't find ,search() return None . find ,search() Method returns a March object .
  • March The object has a group() Method , It returns the actual matching text in the searched string .

reference

1:https://github.com/jackfrued/Python-100-Days

2:Python Programming fast : Automate tedious work / ( beautiful ) Svegart (A1 Sweigart) Writing ; Translated by Wang Haipeng . Beijing : People's post and Telecommunications Press ,2016.7

3:Python Chinese guide ; author : Wang Bingming , edition :v1.0

This sharing is here

A good book is worth reading a hundred times , Read it well and know what it means . Make learning a habit , Use knowledge to change fate , Let the blog witness the growth , Prove your efforts with your actions .

If any of the above is inaccurate , Welcome below Leave a message . Or you have a better idea , Welcome to exchange and study together ~~~


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