python正则表达式

 

1.简单的电话号码匹配

import re
phoneNum = re.compile(r'\d\d\d-\d\d\d-\d\d\d')
mo = phoneNum.search ('my phone number is 562-456-234.')
print('电话是:' + mo.group())
电话是:562-456-234

2.将区号从电话号码中分离

import re
phoneNum = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d)')
mo = phoneNum.search ('my phone number is 562-456-234.')
print('电话第一段是:' + mo.group(1))
print('电话第二段是:' + mo.group(2))
print('电话是:' + mo.group(0))
电话第一段是:562
电话第二段是:456
电话是:562-456-234

3.带有括号的电话,即对特殊字符进行转义

import re
phoneNum = re.compile(r'(\(\d\d\d\))(\d\d\d-\d\d\d)')
mo = phoneNum.search ('my phone number is (562)456-234.')
print(mo.group(1))
print(mo.group(2))
(562)
456-234

4.用管道进行匹配

import re
stro = re.compile(r'Batman|Tina Fey')
mo1 = stro.search('Batman and Tina Fey.')
print(mo1.group())
mo2 = stro.search('Tina Fey and Batman.')
print(mo2.group())
Batman
Tina Fey

5.匹配多个模式中的一个

import re
stro = re.compile(r'Bat(man|mobile|copter)')
mo1 = stro.search('Batmobile lost w wheel.')
print(mo1.group())
print(mo1.group(1))
Batmobile
mobile

6.?表示前面的分组是可选的(匹配零次或一次)

import re
stro = re.compile(r'Bat(wo)?man')
mo1 = stro.search('The adventures of Batman')
print(mo1.group())
mo2 = stro.search('The adventures of Batwoman')
print(mo2.group())
Batman
Batwoman

可用之前的例子

import re
phoneNum = re.compile(r'(\d\d\d-)?(\d\d\d-\d\d\d)')
mo1 = phoneNum.search ('my phone number is 562-456-234.')
print(mo1.group())
mo2 = phoneNum.search('my phone number is 456-234')
print(mo2.group())
562-456-234
456-234

7.*匹配零次或多次

import re
phoneNum = re.compile(r'Bat(wo)*man')
mo1 = phoneNum.search ('The Adventures of Batman')
print(mo1.group())
mo2 = phoneNum.search('The Adventures of Batwoman')
print(mo2.group())
mo3 = phoneNum.search('The Adventures of Batwowowowowowoman')
print(mo3.group())

8.+至少匹配一次或多次

import re
phoneNum = re.compile(r'Bat(wo)*man')
mo1 = phoneNum.search ('The Adventures of Batman')
print(mo1.group())
mo2 = phoneNum.search('The Adventures of Batwoman')
print(mo2.group())
mo3 = phoneNum.search('The Adventures of Batwowowowowowoman')
print(mo3.group())
Batwoman
Batwowowowowowowoman
True

9.{}匹配特定次数

import re
phoneNum = re.compile(r'(Ha){3}')
mo1 = phoneNum.search ('HaHaHa')
print(mo1.group())
mo2 = phoneNum.search('Ha')
print(mo2==None)
HaHaHa
True

10.贪心匹配和非贪心匹配

import re
greedyharegex = re.compile(r'(Ha){3,5}')
mo1 = greedyharegex.search ('HaHaHaHaHa')
print(mo1.group())

nongreedyharegex = re.compile(r'(Ha){3,5}?')
mo2 =nongreedyharegex.search('HaHaHaHaHa')
print(mo2.group())
HaHaHaHaHa
HaHaHa

11. findall()返回所有匹配

import re
phoneNum = re.compile(r'\d\d\d-\d\d\d-\d\d\d')
mo = phoneNum.findall('Call:323-456-345 or 789-456-212')
print(mo)
['323-456-345', '789-456-212']

有分组findall()将返回元组的列表

import re
phoneNum = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d)')
mo = phoneNum.findall('Call:323-456-345 or 789-456-212')
print(mo)
[('323', '456', '345'), ('789', '456', '212')]

12.此外还有的字符分类的缩写代码

常见字符分类的缩写代码
        缩写字符分类            表示
\d0到9的任何数字

\D

除0到9的数字以外的任何字符
\w任何字母、数字或下划线字符(可以认为是匹配“单词”字符)
\W除字母、数字和下划线以外的任何字符
\s空格、制表符或换行符(可以认为是匹配“空白”字符)
\S除空格、制表符和换行符以外的任何字符

举例,\d+\s\w+  即匹配一个或多个数字(\d+),之后是一个空白字符(\s),最后是一个或多个单词(\w+)

13.[]自定义匹配,无需在前面加上斜杠转义

匹配所有大小写元音字母

import re
vow = re.compile(r'[aeiouAEIOU]')
print(vow.findall('RoboCop eats baby food. BABY FOOD.'))
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']

14.[^]在字符分类的左方括号后加上一个插入字符(^),就可以得到“非字符类”

import re
vow = re.compile(r'[^aeiouAEIOU]')
print(vow.findall('RoboCop eats baby food. BABY FOOD.'))
['R', 'b', 'C', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', ' ', 'B', 'B', 'Y', ' ', 'F', 'D', '.']

15.$必须以这种正则表达式结尾

import re
vow = re.compile(r'\d$')
print(vow.findall('his number is 89'))
['9']

16.^\d+$ 匹配从开始到结束都是数字的字符串

import re
vow = re.compile(r'^\d+$')
if vow.search('123456789')==None:
    print("True")
else:
    print("Not True")
if vow.search('1234556xni8990')==None :
    print("True")
else :
    print("Not True")
Not True
True

17. 句号匹配除了换行外所有字符,句号只匹配一个字符

import re
vow = re.compile(r'.at')
print(vow.findall('The car in the hat sat on the flat mat.'))
['hat', 'sat', 'lat', 'mat']

19.(.*)表示任意文本

import re
nongreedy = re.compile(r'<.*?>')
mo1=nongreedy.search('<<The car>in the hat sat on the flat mat.>')
print(mo1.group())
greddy = re.compile(r'<.*>')
mo2=greddy.search('<<The car>in the hat sat on the flat mat.>')
print(mo2.group())
<<The car>
<<The car>in the hat sat on the flat mat.>

20.   .*匹配除换行外所有字符,通过传入re.DOTALL作为re.compile()的第二个参数,可以让句点字符匹配所有字符,包括换行字符

import re
abc1 = re.compile(r'.*')
mo1=abc1.search('Serve the Public trust.\nProtect the innocent.')
print(mo1.group())

abc2 = re.compile(r'.*', re.DOTALL)
mo2=abc2.search('Serve the Public trust.\nProtect the innocent.')
print(mo2.group())
Serve the Public trust.
Serve the Public trust.
Protect the innocent.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值