当前位置:网站首页>\w和[A-Za-z0-9_],\d和[0-9]等价吗?
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
2022-06-27 19:36:00 【JAPAN_is_shit】
开始看正则表达式的时候就有这个疑惑,为什么百度百科上这样说?
这得先明白unicode字符集,当然也可以先搞明白汉字和英文以及数字是什么字符集?Unicode 字符百科
汉字在unicode表中是4e00-9fa5
英文,数字和特殊符号属于unicode中的拉丁文
因此\w就比[A-Za-z0-9_]的范围大得多,比如能匹配其他国家的文字,而\d能匹配其他国家的数字。
不止\w和\d的范围是这样,正则元字符中的 . \W,\D,\s,\S,\b,\B也能匹配到其他的文字,那怎样不匹配到所有的Unicode字符呢?
通过re.ASCII来设置只匹配ASCII字符
import re
#扩充阿拉伯-印度文数字
s="۱۲۳۴۵۶۷۸۹"
print(s.isdigit())
a= re.match(r'\d+', s)
print(a.group())
#True
#蒙古文
d = 'ᠠᠡᠢᠣᠤᠶᠿ'
b= re.match(r'\w+', d) #匹配字母数字下划线
print(b.group())
#۱۲۳۴۵۶۷۸۹
#蒙古文
d = 'ᠠᠡᠢᠣᠤᠶᠿ'
b= re.match(r'\D+', d) #匹配一个非数字
print(b.group())
#ᠠᠡᠢᠣᠤᠶᠿ
#蒙古文
d = 'ᠠᠡᠢᠣᠤᠶᠿ'
b= re.match(r'\S+', d) #匹配一个可见字符
print(b.group())
#ᠠᠡᠢᠣᠤᠶᠿ
s="۱۲۳۴۵۶۷۸۹"
print(s.isdigit())
a= re.match(r'.+', s)
print(a.group())
#蒙古文
d = 'ᠠᠡᠢᠣᠤᠶᠿᠢᠣᠤ'
b= re.findall(r'\bᠠᠡ', d) #匹配一个单词边界
print(b)
# ['ᠠᠡ']
经过re.ASCII设置, \w再去匹配蒙古文是匹配不到任何东西的
#蒙古文
d = 'ᠠᠡᠢᠣᠤᠶᠿᠢᠣᠤ'
b= re.findall(r'\wᠠᠡ', d,re.ASCII)#匹配一个单词边界,限定在ASCII中
print(b)
# [] 匹配不到任何东西
边栏推荐
- Go from starting to Real - Interface (note)
- Go from entry to practice -- CSP concurrency mechanism (note)
- Go from introduction to practice -- coordination mechanism (note)
- GBase 8a OLAP分析函数 cume_dist的使用样例
- Go from introduction to practice - Interface (notes)
- excel读取文件内容方法
- Gbase 8A OLAP analysis function cume_ Example of dist
- Common methods of string class
- [LeetCode]515. 在每个树行中找最大值
- Go从入门到实战——依赖管理(笔记)
猜你喜欢

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

Go from introduction to actual combat - context and task cancellation (notes)

Go从入门到实战——Panic和recover(笔记)

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

win11桌面出現“了解此圖片”如何删除

Go from introduction to practice -- coordination mechanism (note)

读写分离-Mysql的主从复制

Go从入门到实战——CSP并发机制(笔记)
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]
随机推荐
GBase 8a OLAP函数group by grouping sets的使用样例
[LeetCode]508. 出現次數最多的子樹元素和
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
gomock mockgen : unknown embedded interface
[leetcode] dynamic programming solution split integer i[silver fox]
根据自定义excel标题模板快速excel导出
Null pointer exception
快速excel导出
动态刷新mapper看过来
A method of go accessing gbase 8A database
鲜为人知的mysql导入数据
Go从入门到实战——任务的取消(笔记)
Go from entry to practice - dependency management (notes)
流程控制任务
软件缺陷管理——测试人员必会
Example of using gbase 8A OLAP function group by grouping sets
美团20k软件测试工程师的经验分享
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
GBase 8a数据库用户密码安全相关参数汇总
Go从入门到实战——依赖管理(笔记)