日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python自动化之正则

發布時間:2023/12/2 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python自动化之正则 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

?

#################貪心和非貪心匹配####################################

##########正則表達式:貪心,在有二義的情況下,盡可能匹配最長的字符串###

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()方法###################################

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

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? 表示0到9的任何數字

###\D? 表示除0到9的數字之外的任何字符

###\w? 表示任何字母、數字或下劃線字符(可以認為是匹配“單詞”字符)

###\W? 表示除字母、數字和下劃線以外的任何字符

###\s? 表示空格、制表符或換行符(可以認為是匹配“單詞”字符)

###\S? 表示除空格、制表符或換行符以外的任何字符

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 partridge')

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

vowelRegex=re.compile(r'[aeiouAEIOU]')

vowelRegex.findall('RoboCop eats baby food.BABY FOOD')

?

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

#########^表明匹配必須發生在被查找文本開始處#########################

#########$表明該字符串必須匹配該模式結束#############################

?

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

#########.(句點)字符被稱為"通配符"###################################

atRegex=re.compile(r'.at')

atRegex.findall('The cat in the hat sat on the flat mat')

?

###########################用點-星匹配所有字符######################

######點-星(.*)表示"任意文本"(貪心模式:總是匹配盡可能多的文本)######

####點-星-問號表示“任意文本”(非貪心模式:總是匹配盡可能少的文本)####

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()

?

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

###############點-星將匹配除換行以外的所有字符#########################

###############通過傳入re.DOTALL作為re.compile()的第二個參數###########

###############可以讓句點字符匹配所有字符,包括換行字符#################

?

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

##############向re.compile()傳入re.IGNORECASE或re.I,不區分大小寫#######

?

?

#############用sub()方法替換字符串#####################################

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 was 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:

####正則表達式,匹配每3位就有一個逗號的數字?必須匹配以下數字:

.'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()

轉載于:https://www.cnblogs.com/dudumiaomiao/p/6837621.html

總結

以上是生活随笔為你收集整理的python自动化之正则的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。