python里使用正则表达式的全匹配功能

前面学习了很多匹配,比如搜索任意位置的search()函数,搜索边界的match()函数,现在还需要学习一个全匹配函数,就是搜索的字符与内容全部匹配,它就是fullmatch()函数。例子如下:
#python 3.6
#蔡军生 
#http://blog.csdn.net/caimouse/article/details/51749579
#
import re


text = 'This is some text -- with punctuation.'
pattern = 'is'


print('Text       :', text)
print('Pattern    :', pattern)


m = re.search(pattern, text)
print('Search     :', m)
s = re.fullmatch(pattern, text)
print('Full match :', s)




text = 'is'
print('Text       :', text)
s = re.fullmatch(pattern, text)
print('Full match :', s)


text = 'iss'
print('Text       :', text)
s = re.fullmatch(pattern, text)
print('Full match :', s)

结果输出如下:

Text       : This is some text -- with punctuation.
Pattern    : is
Search     : <_sre.SRE_Match object; span=(2, 4), match='is'>
Full match : None
Text       : is
Full match : <_sre.SRE_Match object; span=(0, 2), match='is'>
Text       : iss
Full match : None

深入浅出Numpy
http://edu.csdn.net/course/detail/6149 

Python游戏开发入门

你也能动手修改C编译器

纸牌游戏开发

http://edu.csdn.net/course/detail/5538 

五子棋游戏开发

http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通 
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战 
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用 
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672


Python使用正则表达式匹配中文字符,你可以使用Unicode属性或者特定的Unicode范围来匹配。中文字符一般位于Unicode编码的Basic Multilingual Plane (BMP)中的CJK统一汉字区域,其编码范围大致是从`\u4e00`到`\u9fff`。以下是一些示例代码,展示了如何使用正则表达式匹配中文字符: ```python import re # 使用Unicode范围匹配中文字符 pattern = r'[\u4e00-\u9fff]' text = "这是一个包含中文字符的字符串:中文" matches = re.findall(pattern, text) print(matches) # 输出匹配到的中文字符列表 # 使用Unicode属性 \p{L} 匹配任何语言的字母字符,包括中文 pattern = r'\p{L}' text = "这是一个包含中文字符的字符串:中文" matches = re.findall(pattern, text) print(matches) # 输出匹配到的中文字符列表 # 使用re.UNICODE或re.U标志,这样\w、\W、\b、\B、\d、\D、\s和\S会匹配所有字符 pattern = r'\w' text = "这是一个包含中文字符的字符串:中文" matches = re.findall(pattern, text, flags=re.UNICODE) print(matches) # 输出匹配到的中文字符和字母数字字符列表 # 注意:在Python 3中,正则表达式模块默认就是支持Unicode的 ``` 在使用正则表达式匹配中文字符时,你需要注意Python版本和库对Unicode的支持。在Python 3中,正则表达式模块默认支持Unicode,而Python 2可能需要在模式字符串中加入`u`前缀来确保模式字符串被作为Unicode字符串处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值