python RE 的使用

PYTHON 的 RE 模块理解(RE.COMPILE、RE.MATCH、RE.SEARCH)
import re
help(re.compile)
'''
输出结果为:
Help on function compile in module re:

compile(pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.
通过 help 可知:编译一个正则表达式模式,返回一个模式对象。
'''

'''
第二个参数 flags 是匹配模式,可以使用按位或’|’表示同时生效,也可以在正则表达式字符串中指定。
Pattern 对象是不能直接实例化的,只能通过 compile 方法得到。匹配模式有:
1).re.I(re.IGNORECASE): 忽略大小写
2).re.M(MULTILINE): 多行模式,改变’^’和’$’的行为
3).re.S(DOTALL): 点任意匹配模式,改变’.’的行为
4).re.L(LOCALE): 使预定字符类 \w \W \b \B \s \S 取决于当前区域设定
5).re.U(UNICODE): 使预定字符类 \w \W \b \B \s \S \d \D 取决于 unicode 定义的字符属性
6).re.X(VERBOSE): 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释
'''

text=“JGod is a handsome boy ,but he is a ider”
print re.findall(r’\wo\w’,text) #查找有 o 的单词
#输出结果为:[‘JGod’, ‘handsome’, ‘boy’]

#利用 compile 生成一个规则模式吧,然后利用 findall 将某一个对象内容进行匹配。,合适则输出符合规则的内容
regex=re.compile(r’\wo\w’)
print regex.findall(text)
#>>> [‘JGod’, ‘handsome’, ‘boy’]

test1=“who you are,what you do,When you get get there? What is time you state there?”
regex1=re.compile(r’\wwh\w’,re.IGNORECASE)
wh=regex1.findall(test1)
print wh
#>>> [‘who’, ‘what’, ‘When’, ‘What’]

'''
re 正则表达式模块还包括一些有用的操作正则表达式的函数。下面主要介绍 match 函数以及 search 函数。
定义: re.match 尝试从字符串的开始匹配一个模式。
原型:
re.match(pattern, string, flags)
第一个参数是正则表达式, 如果匹配成功,则返回一个 Match,否则返回一个 None;
第二个参数表示要匹配的字符串;
第三个参数是标致位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。

函数的返回值为真或者假。
例如:match(‘p’,’python’) 返回值为真;match(‘p’,’www.python.org’) 返回值为假。

定义:re.search 会在给定字符串中寻找第一个匹配给定正则表达式的子字符串。

函数的返回值:如果查找到则返回查找到的值,否则返回为 None。

原型:
re.search(pattern, string, flags)

每个参数的含意与 re.match 一样。
'''
#re.match 的例子 1
import re
your_love=re.match(“wh”,“What are you doing? who is you mate?”,re.I)
if your_love:
print “you are my angle”
else:
print “i lose you "
#相当于:
print “*”*100 #便于区分
import re
content=“What are you doing? who is your mate?”
regu_cont=re.compile(”\wwh\w",re.I)
yl=regu_cont.match(content)
if yl:
print yl.group(0)
else:
print “what happen?”
解析:首先创造了需要正则表达式匹配的字符串 content;
接着利用 re.compile() 来创建了我们所需要的匹配规则,创建了模式对象 regu_cont;
yl 用来接收对内容 content 字符串进行 regu_cont 正则表达式实现 match 函数的结果
如果有 yl 不为空,则使用 m.group(index) 输出查找到的子字符串
否则(返回值为 None) print “what happen?”

match 例子 2

'''
match 如果查找到结果, 将返回一个 MatchObject,你可以查询 MatchObject 关于匹配字符串的相关信息了。MatchObject 实例也有几个方法和属性;最重要的那些如下所示:
group() 返回被 RE 匹配的字符串
start() 返回匹配开始的位置
end() 返回匹配结束的位置
span()返回一个元组包含匹配 ( 开始, 结束) 的位置
'''
import re
content=“What are you doing? who is your mate?”
regu_cont=re.compile(“\wwh\w”,re.I)
yl=regu_cont.match(content)
if yl:
print yl.group(0)
else:
print “pass the test”
print yl.group()
print yl.start()
print yl.end()
print yl.span()

执行结果为:
What
What
0
4
(0, 4)

#search()方法与 match() 方法类似
import re
content=‘Where are you from? You look so hansome.’
regex=re.compile(r’\wsom\w’)
m=regex.search(content)
if m:
print m.group(0)
else:
print “Not found”

#相当于:
import re
m=re.search(r’\wsom\w’,‘Where are you from? You look so handsome.’,re.I)
if m:
print m.group(0)
else:
print “not found”

转自:[https://www.cnblogs.com/papapython/p/7482349.html](https://www.cnblogs.com/papapython/p/7482349.html)