The regular of Python automation

Source: Internet
Author: User

Import re

Phonenumregex=re.compile (R ' \d\d\d-\d\d\d-\d\d\d\d ')

Mo=phonenumregex.search (' My number is 415-555-4242. ')

Print (' Phone number found: ' +mo.group ())

###### #利用括号分组 ##############

Phonenumregex=re.compile (R ' (\d\d\d)-(\d\d\d-\d\d\d\d))

Mo=phonenumregex.search (' My number is 415-555-4242. ')

Mo.group (1)

Mo.group (2)

Mo.group (0)

Mo.group ()

Mo.groups ()

Areacode,mainnumber=mo.groups ()

AreaCode

Mainnumber

############### #用管道匹配多个分组 #######################

Heroregex=re.compile (R ' batman| Tina Fey ')

Mo1=heroregex.search (' Batman and Tina Fey ')

Mo1.group ()

Mo2=heroregex.search (' Tina Fey and Batman ')

Mo2.group ()

Batregex=re.compile (R ' Bat (Man|mobile|copter|bat) ')

Mo=batregex.search (' Batmobile lost a wheel ')

Mo.group ()

Mo.group (1)

############### #用问号实现可选匹配 #######################

Batregex=re.compile (R ' Bat (wo)? man ')

Mo1=batregex.search (' The Adventures of Batman ')

Mo1.group ()

Mo2=batregex.search (' The Adventures of Batwoman ')

Mo2.group ()

Phoneregex=re.compile (R ' (\d\d\d-)? \d\d\d-\d\d\d ')

Mo1=phoneregex.search (' My number is 415-555-4242 ')

Mo1.group ()

Mo2=phoneregex.search (' My number is 555-4242 ')

Mo2.group ()

############### #用星号匹配零次或多次 ###################

Batregex=re.compile (R ' Bat (wo) *man ')

Mo1=batregex.search (' The Adventures of Batman ')

Mo1.group ()

Mo2=batregex.search (' The Adventures of Batwoman ')

Mo2.group ()

Mo3=batregex.search (' The Adventures of Batwowowoman ')

Mo3.group ()

############### #用加号匹配一次或多次 ###################

Batregex=re.compile (R ' Bat (wo) +man ')

Mo1=batregex.search (' The Adventures of Batman ')

Mo1.group ()

Mo2=batregex.search (' The Adventures of Batwoman ')

Mo2.group ()

Mo3=batregex.search (' The Adventures of Batwowowoman ')

Mo3.group ()

############### #用花括号匹配特定次数 ######################

Haregex=re.compile (R ' (Ha) {3} ')

Mo1=haregex.search (' hahaha ')

Mo1.group ()

Mo2=haregex.search (' Ha ')

Mo2==none

################ #贪心和非贪心匹配 ####################################

######### #正则表达式: Greedy, in the case of two righteousness, match as long as possible string # # #

Greedyharegex=re.compile (R ' (Ha) {3,5} ')

Mo1=greedyharegex.search (' hahaha ')

Mo1.group ()

Nongreedyharegex=re.compile (R ' (Ha) {3,5}? ')

Mo2=nongreedyharegex.search (' Hahahaha ')

Mo2.group ()

################### #findall () method ###################################

########## #包含被查找字符串中的所有匹配 #############################

Phonenumregex=re.compile (R ' \d\d\d-\d\d\d-\d\d\d\d ')

Phonenumregex.findall (' cell:415-555-9999 work:215-555-0000 ')

########################## #字符分类 #################################

###\d represents any number from 0 to 9

###\d represents any character other than a number from 0 to 9

###\w represents any letter, number, or underscore character (which can be considered a matching "word" character)

###\w represents any character except letters, numbers, and underscores

###\s represents a space, tab, or newline character (which can be thought of as matching "word" characters)

###\s represents any character except spaces, tabs, or newline characters

Xmasregex=re.compile (R ' \d+\s\w+ ')

xmasRegex.findall(‘12 drummers,11 pipers,10 lords,9 ladies,8 maids,7 swans,6 geese,5 rings,4 birds,3 hens,2 doves,1 partri Dge ')

###################### #建立自己的字符分类 ###########################

Vowelregex=re.compile (R ' [Aeiouaeiou] ')

Vowelregex.findall (' RoboCop eats baby food. BABY food ')

####################### #插入字符和美元字符 ###########################

#########^ indicates that the match must occur at the beginning of the text being searched #########################

#########$ indicates that the string must match the end of the pattern #############################

########################## #通配字符 ##################################

#########. The (period) character is called a "wildcard" ###################################

Atregex=re.compile (R '. at ')

Atregex.findall (' The Cat in the hat sat on the flat mat ')

########################## #用点-Star matches all characters ######################

##### #点-Star (. *) denotes "arbitrary text" (Greedy mode: Always matches as much text as possible) ######

# # # #点-Star-question mark denotes "arbitrary text" (non-greedy mode: Always matches as little text as possible) # # # # # #

Nongreedyregex=re.compile (R ' <.*?> ')

Mo=nongreedyregex.search (' <to serve man> for dinner.> ')

Mo.group ()

Greedyregex=re.compile (R ' <.*> ')

Mo=greedyregex.search (' <to serve man> for dinner.> ')

Mo.group ()

#######################################################################

############## #点-The star will match all characters except newline #########################

############## #通过传入re. Dotall as the second parameter of Re.compile () ###########

############## #可以让句点字符匹配所有字符, including newline characters #################

#######################################################################

############# #向re. Compile () incoming re. IgnoreCase or RE.I, case insensitive #######

############ #用sub () method replaces the string #####################################

Namesregex=re.compile (R ' Agent \w+ ')

Namesregex.sub (' censored ', ' agent Alice gave the secret documents to Agent Bob. ')

Agentnamesregex=re.compile (R ' Agent (\w) \w* ')

Agentnamesregex.sub (R ' \1**** ', ' agent Alice told agent Carol that agent Eve knew Agent Bob is a double agent. ')

################### #管理复杂的正则表达式 ###############################

Phoneregex=re.compile (R "" (

(\d{3}|\ (\d{3}\))? #area Code

(\s|-|\.)? #separator

\D{3} #first 3 digits

(\s|-|\.) #separator

\D{4} #last 4 digits

(\s* (Ext|x|ext.)  \s*\d{2,5})? #extension

) ", Re. VERBOSE)

#########################################################################

##################### #pyperclip模块复制和粘贴字符串 ######################

Import Pyperclip,re

# # # #Create Phone regex

Phoneregex=re.compile (R "" (

(\d{3}|\ (\d{3}\))? #area Code

(\s|-|\.)? #separator

\D{3} #first 3 digits

(\s|-|\.) #separator

\D{4} #last 4 digits

(\s* (Ext|x|ext.)  \s*\d{2,5})? #extension

) ", Re. VERBOSE)

# # # #Create Email regex

Emailregex=re.compile (R "" (

[A-za-z0-9._%+-]+ #username

@ #@ Symbol

[s-za-z0-9.-]+ #domain Name

(\. [A-za-z] {2,4}) #dot-something

) ", Re. VERBOSE)

# # # #Find matches in clipboard text.

Text=str (Pyperclip.paste ())

Matches=[]

For groups in Phoneregex.findall (text):

Phonenum = '-'. Join ([groups[1],groups[3],groups[5]])

If groups[8]! = ":

Phonenum + = ' x ' +groups[8]

Matches.append (Phonenum)

# # # #Copy results to the Clipboard.

If Len (matches) >0:

Pyperclip.copy (' \ n '. Join (matches))

Print (' Copied to clipboard: ')

Print (' \ n '. Join (matches))

Else

Print (' No phone numbers or email addresses found. ')

# # #示例1:

############################# #强口令检测 ###################

################## #长度不少于8个字符 #######################

################## #同时包含大写和小写字符 ##################

################## #至少有一位数字 ##########################

Import re

def checklength (PWD):

Ifornot=len (PWD)

If ifornot>=8:

Return True

Else

Return False

def checkupperletter (PWD):

Upperletter=re.compile (R ' [a-z]+ ')

Mo=upperletter.search (PWD)

If MO:

Return True

Else

Return False

def checklowerletter (PWD):

Lowerletter=re.compile (R ' [a-z]+ ')

Mo=lowerletter.search (PWD)

If MO:

Return True

Else

Return False

def checknumletter (PWD):

Lowerletter=re.compile (R ' [0-9]+ ')

Mo=lowerletter.search (PWD)

If MO:

Return True

Else

Return False

def checkpassword (PWD):

Return (Checklength (PWD) and Checkupperletter (PWD) and Checklowerletter (PWD) and Checknumletter (PWD)

# # #示例2:

# # # #正则表达式, match every 3 bits there is a comma number? The following numbers must be matched:

.‘ 42 '

.‘ 1,234 '

.‘ 6,368,745 '

# # # #但不会匹配:

.‘ 12,34,567 '

.‘ 1234 '

Numregex=re.compile (R ' ^\d{1,3} (, \d{3}) *$ ')

Mo=numregex.search (' 12,304,567 ')

Mo.group ()

The regular of Python automation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.