Python 正则表达式(对爬虫特别有用)举例

视频笔记:Python 基础教程 (莫烦 Python 教程),链接如下

https://www.bilibili.com/video/av16926522/?p=36

#导入Python正则表达式模块

>>> import re  

#简单的python匹配

>>> # matching string

>>> pattern1 = 'cat'

>>> pattern2 = 'bird'

>>> string = 'dog runs to cat'

>>> print(pattern1 in string)

>>> print(pattern2 in string)

True

False

 

#用正则寻找匹配

>>> #regualr expression

>>> pattern1 = 'cat'

>>> pattern2 = 'bird'

>>> string = 'dog runs to cat'

>>>print(re.search(pattern1,string))

>>>print(re.search(pattern2,string))

<re.Match object; span=(12, 15), match='cat'>

None

 

#匹配多种可能 使用[]

>>>ptn = r"r[au]n" #前面加个r,意味着后面不是字符串,是表达式的形式

>>>print(re.search(ptn, 'dog runs to cat'))

>>>print(re.search(ptn, 'dog ran to cat'))

<re.Match object; span=(4, 7), match='run'>

<re.Match object; span=(4, 7), match='ran'>

 

#匹配更多种可能  x-x

#continue

>>> print(re.search(r"r[A-Z]n", "dog runs to cat"))      

None

>>> print(re.search(r"r[a-z]n", "dog runs to cat"))    

<re.Match object; span=(4, 7), match='run'>

>>> print(re.search(r"r[0-9]n", "dog r2ns to cat"))      

<re.Match object; span=(4, 7), match='r2n'>

>>> print(re.search(r"r[0-9a-z]n", "dog runs to cat"))      

<re.Match object; span=(4, 7), match='run'>

 

#特殊种类匹配

#数字 d

# \d : decimal digit

>>> print(re.search(r"r\dn","run r4n"))     

<re.Match object; span=(4, 7), match='r4n'>

# \D : any non-decimal digit

>>> print(re.search(r"r\Dn","run r4n"))      

<re.Match object; span=(0, 3), match='run'>

 

#空白 s

# \s : any white space [\t\n\r\f\v]

# \t tab \n回车 ~~~

>>> print(re.search(r"r\sn","r\nn r4n"))      

<re.Match object; span=(0, 3), match='r\nn'>

# \S : any non-white space

>>> print(re.search(r"r\Sn","r\nn r4n"))      

<re.Match object; span=(4, 7), match='r4n'>

 

#所有字母数字和”_”

# \w : [a-zA-Z0-9_]

>>> print(re.search(r"r\wn","r\nn r4n"))     

<re.Match object; span=(4, 7), match='r4n'>

# \W : opposite to \w

>>> print(re.search(r"r\Wn","r\nn r4n"))      

<re.Match object; span=(0, 3), match='r\nn'>

 

#空白字符 b

# \b: empty string (only at the start or end of the word)

>>> print(re.search(r"\bruns\b","dog runs to cat"))      

<re.Match object; span=(4, 8), match='runs'>

# \B: empty string (but not at the start or end of a word)

>>> print(re.search(r"\Bruns\B","dogrunsto cat"))

<re.Match object; span=(3, 7), match='runs'>

 

#特殊字符\\ 任意字符.

# \\ : match \

>>> print(re.search(r"runs\\","runs\ to me"))

<re.Match object; span=(0, 5), match='runs\\'>

# . : match anything (except \n)

>>> print(re.search(r"r.n","r[ns to me"))     

<re.Match object; span=(0, 3), match='r[n'>

 

#句首^ 句尾$

# ^ : match line beginning

>>> print(re.search(r"^dog","dog runs to cat"))

<re.Match object; span=(0, 3), match='dog'>

# $ : match line ending

>>> print(re.search(r"cat$","dog runs to cat"))

<re.Match object; span=(12, 15), match='cat'>

 

# 是否 ?

# ? : may or may not occur

>>> print(re.search(r"Mon(day)?","Monday"))

<re.Match object; span=(0, 6), match='Monday'>

>>> print(re.search(r"Mon(day)?","Mon"))

<re.Match object; span=(0, 3), match='Mon'>

 

#多行匹配 flags=xx.M

# multi-line

>>>string1 = """

>>>dog runs to cat.

>>>I run to dog.

>>>"""

>>>print(re.search(r"^I",string1)) #匹配句首I

None

>>>print(re.search(r"^I",string1,flags=re.M)) #M:MULTILLINE把每一句都看成单独的句子

<re.Match object; span=(18, 19), match='I'>

 

#出现0或多次 *

# * : occur 0 or more times

>>>print(re.search(r"ab*","a"))

<re.Match object; span=(0, 1), match='a'>

>>>print(re.search(r"ab*","abbbbb"))

<re.Match object; span=(0, 6), match='abbbbb'>

 

#出现1或多次 +

# + : occur 1 or more times

>>>print(re.search(r"ab+","a"))

None

>>>print(re.search(r"ab+","abbbbb"))

<re.Match object; span=(0, 6), match='abbbbb'>

 

#可选次数 {n,m}

# {n,m} : occur n to m times

>>>print(re.search(r"ab{2,10}","a"))

None

>>>print(re.search(r"ab{2,10}","abbbb"))

<re.Match object; span=(0, 5), match='abbbb'>

 

# group组

# group \d+ 数字,一个或多个 .+所有字符,一个或多个

>>>match = re.search(r"(\d+),Date:(.+)","ID:021523,Date:Fed/12/2017")

>>>print(match.group()) #所有都返回

>>>print(match.group(1)) #第一个括号的

>>>print(match.group(2)) #第二个括号的

021523,Date:Fed/12/2017

021523

Fed/12/2017

# group加名字(?P<xx>xxx)

>>>match = re.search(r"(?P<id>\d+),Date:(?P<date>.+)","ID:021523,Date:Fed/12/2017")

>>>print(match.group('id'))

>>>print(match.group('date'))

>>>print(match.group(2))

021523

Fed/12/2017

Fed/12/2017

 

#寻找所有匹配 findall

# findall

>>> print(re.findall(r"r[ua]n","run ran ren"))

['run', 'ran']

# | : or

>>> print(re.findall(r"(run|ran)","run ran ren"))     

['run', 'ran']

 

#替换 re.sub()

# re.sub() replace

>>> print(re.sub(r"r[au]ns","catches","dog runs to cat"))      

dog catches to cat

 

#分裂 re.split() .任何东西,\.符号.

>>> print(re.split(r"[,;\.]","a;b,c.d;e"))      

['a', 'b', 'c', 'd', 'e']

# compile 编译

>>> compiled_re = re.compile(r"r[ua]n");      

>>> print(compiled_re.search("dog ran to cat"))      

<re.Match object; span=(4, 7), match='ran'>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值