2019年3月6日 星期三

[Python] 正規表達式範例

正規表達式範例


find numbers

 
import re
# find numbers
pattern = "[0-9]+"
string = '12 drummers drumming, 111 pipers piping, 1006 lords
a-leaping'
re.findall(pattern, string)
# ['12', '111', '1006']
# find letters
pattern = "[cmf]an"
string = 'find: can, man, fan, skip: dan, ran, pan'
re.findall(pattern, string)
#
['can', 'man', 'fan']

find e-mail

 
import re
email_text = """
Big Data Analytics/ Deep LearningSocial Computing / Computational Social Science / Crowdsourcing
Multimediaand Network SystemsQuality of ExperienceInformation SecurityPh.D. candidate at NTU
EEchihfan02-27883799#1602Camera CalibrationComputer VisionData
Analysiscmchang02-27883799#1671System OptimizationMachine LearningyusraBig data
analysiscclin02-27883799#1668Data Analysisrusi02-27883799#1668Government Procurement ActFinancial
Managementkatekuen02-27883799#1602AdministrationEvent Planningseanyu02-27883799#1668Data
AnalysisPsychology & NeuroscienceMarketingxinchinchenEmbedded Systemkyoyachuan062602-27883799
#1601FinTechActuarial ScienceData Analysiskai0604602-27883799#1601Data AnalysisMachine
Learningchloe02-27839427Accountingafun02-27883799 felix2018@iis.sinica.edu.tw
#1673Data AnalysisWeb developmentyunhsu198902-27883799#1668MarketingTIGP Ph.D. Fellow at Academia Sinica &
NCCUbaowalyMachine LearningData AnalysisSocial Computingchangyc1427883799#1678
Data Analysisjimmy1592302-2788379 jimmy15923@iis.sinica.com.tw#1688Data AnalysisjasontangAnalysisMachine
Learninguchen02-27883799#1668Deep Learningpjwu02-27883799#1604Computational PhotographyData Analysis
"""
 re.findall("([A-Za-z0-9._]+@[A-Za-z.]+[com|edu]\.tw)", email_text)
 # Output:
['felix2018@iis.sinica.edu.tw', 'jimmy15923@iis.sinica.com.tw']

find e-mail

 
Q1. 同時匹配 abcdefg, abcde, abc
Q2. 同時匹配 abc123xyz, abcde22a, abc456aaa
Q3. 匹配 "catcat"
Q4. 同時匹配 wazzzzzup, wazzzup
Q5. 同時匹配 aaaabcc, aabbbbc, aacc
Q6. 匹配手機號碼, 格式為:0987-654-321
Q7. 匹配右方格式, xxx.xxx.xxx.xxx (其中 x 是 0-9 的數字)

Q1. [a-z]*  
Q2. [a-z]*[0-9]*[a-z]*  
Q3. \"(cat){2}\"  
Q4. wa(z{3}|z{5})up 或 waz{3,5}up  
Q5. [abc]+  
Q6. [0-9]{4}\-[0-9]{3}\-[0-9]{3}  
Q7. [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}  或  ([0-9]{1,3}\.){3}[0-9]{1,3}

沒有留言:

張貼留言