前面学习了很多搜索的方式,其实对正则表达式的处理引擎是没有进行过设置参数的,在这里来学习一下怎么样设置,所选择的flag参数都会按或的方式添加到正则表达式里,然后再给函数compile(),search(),match()进行执行,如下例子,就来让正则表达式不对字符的大小在意,使用IGNORECASE参数就可以了。如下:
结果输出如下:
Text:
'This is some text -- with punctuation.'
Pattern:
\bT\w+
Case-sensitive:
'This'
Case-insensitive:
'This'
'text'
深入浅出Numpy
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import re
text = 'This is some text -- with punctuation.'
pattern = r'\bT\w+'
with_case = re.compile(pattern)
without_case = re.compile(pattern, re.IGNORECASE)
print('Text:\n {!r}'.format(text))
print('Pattern:\n {}'.format(pattern))
print('Case-sensitive:')
for match in with_case.findall(text):
print(' {!r}'.format(match))
print('Case-insensitive:')
for match in without_case.findall(text):
print(' {!r}'.format(match))
结果输出如下:
Text:
'This is some text -- with punctuation.'
Pattern:
\bT\w+
Case-sensitive:
'This'
Case-insensitive:
'This'
'text'