Python 模块RE整理

python 模块RE正则表达式模块整理

notice:

  • python 中的字符串分为两种,一种由8位的ASCALL码字符组成字符串,另一种为Unicode字符串,在匹配的过程中二者不能混用,只能同种类型的情况进行匹配。
  • 正则表达式使用反斜杠” \ “来代表特殊形式或用作转义字符,这里跟Python的语法冲突,因此,Python用” \\ “表示正则表达式中的” \ “,因为正则表达式中如果要匹配” \ “,需要用\来转义,变成” \ “,而Python语法中又需要对字符串中每一个\进行转义,所以就变成了” \\ “。为了使正则表达式具有更好的可读性,Python特别设计了原始字符串(raw string),raw string就是用’r’作为字符串的前缀,如 r”\n”:表示两个字符”\”和”n”,而不是换行符。Python中写正则表达式时推荐使用这种形式。

Python中re模块中的特殊字符

. 匹配任意字符
[] 用来匹配一个指定的字符类别,所谓的字符类别就是你想匹配的一个字符集,对于字符集中的字符可以理解成或的关系
^ 对于字符串,表示字符串的开头
对于^加上一个其他数字或字符,表示取反。比如,[^5]表示除了5之外的任意字符。[^^]表示除了^字符之外任意字符。
$ 匹配字符串的末尾,或者匹配换行之前的字符串末尾
* 对于前一个字符重复0到无穷次
+ 对于前一个字符重复1到无穷次
? 对于前一个字符重复0到1次
{m,n} 对于前一个字符重复次数在为m到n次。
{0,} == *
{1,} ==
{0,1} == ?
{m} 对于前一个字符重复m次

python中re模块中特殊转义序列(字符)

\A 匹配字符串的开头
\b 匹配一个空字符(仅对一个单词word的开始或结束有效)
\B 与\b含义相反
\d 匹配任何十进制数;它相当于类 [0-9]
\D 匹配任何非数字字符;它相当于类 [^0-9]
\s 匹配任何空白字符;它相当于类 [ \t\n\r\f\v]
\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]
\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]
\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
\Z 匹配字符串的结尾

相关函数

re.match(pattern, string, flags=0)

re.match 尝试从字符串的开始匹配一个模式,也就是从字符串开头开始匹配,Try to apply the pattern at the start of the string, returning a match object, or None if no match was found

re.search(pattern, string, flags=0)

 re.search函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回,如果字符串没有匹配,则返回None。Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.

re.findall(pattern, string, flags=0)

re.findall可以获取字符串中所有匹配的字符串。Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group,Empty matches are included in the result.

#!/usr/bin/python2.7
# -*- ecoding:utf-8 -*-
# 支持 python3.x, python2.x 去掉print后的括号 
# 演示()以及 match, search, findall 异同
import re



text = "They have coffee 123 handsome , should be almost ... as well"
m = re.match(r"(\w+)", text)
if m:
    print (m.group(0))
else:
    print ('not match')

m = re.match(r"(\w+)(\W+)(\w+)", text)
if m:
    print (m.group(0))
else:
    print ('not match')

m = re.search(r"(\d\w+)", text)
if m:
    print (m.group(0))
else:
    print ('not match')
m = re.search(r"\d(\w+)(\W+)(\w+)", text)
if m:
    print (m.group(0))
else:
    print ('not match')

m = re.findall(r'(\d+\s)', text)
print (m)
m = re.findall(r"(\w+)", text)
print (m)
m = re.findall(r"(\w+)(\W+)(\w+)", text)
print (m)
'''
#output:
They
They have
123
123 handsome
['123 ']
['They', 'have', 'coffee', '123', 'handsome', 'should', 'be', 'almost', 'as', 'well']
[('They', ' ', 'have'), ('coffee', ' ', '123'), ('handsome', ' , ', 'should'), ('be', ' ', 'almost'), ('as', ' ', 'well')]
'''

简单的html页面中 url匹配

#!/usr/bin/python2.7
# -*- ecoding:utf-8 -*-
# 支持 python3.x, python2.x 去掉print后的括号 

import re

context = '''
<div class="list_item article_item">
            <div class="article_title">
                <span class="ico ico_type_Original"></span>
                <h1>
                    <span class="link_title"><a href="/u011461385/article/details/52448295">
                    tettt
                    </a></span>
                </h1>
             </div>
            <div class="article_description"> tttttttttt...        </div>
            <div class="article_manage">
                <span class="link_postdate">2016-09-06 11:21</span>
                <span class="link_view" title="阅读次数"><a href="/u011461385/article/details/52448295" title="阅读次数">阅读</a>(5)</span>
                <span class="link_comments" title="评论次数"><a href="/u011461385/article/details/52448295#comments" title="评论次数" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
            </div>
            <div class="clear"></div>
        </div>
'''

pattern1 = r'<div class="(\S+)"'
listed = re.findall(pattern1, context)
print (listed)

pattern2= r'<a href="([\S]*?)"'
listed = re.findall(pattern2, context)
print (listed)
pattern2= r'<a href="(.*?)"'
listed = re.findall(pattern2, context)
print (listed)
pattern2= r'<a href="(\S+)"'
listed = re.findall(pattern2, context)

'''
#output:
['article_title', 'article_description', 'article_manage', 'clear']
['/u011461385/article/details/52448295', '/u011461385/article/details/52448295', '/u011461385/article/details/52448295#comments']
['/u011461385/article/details/52448295', '/u011461385/article/details/52448295', '/u011461385/article/details/52448295#comments']
['/u011461385/article/details/52448295', '/u011461385/article/details/52448295', '/u011461385/article/details/52448295#comments']

'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值