当前位置:网站首页>\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从入门到实战——Context与任务取消(笔记)
- Gbase 8A OLAP analysis function cume_ Example of dist
- Go from introduction to actual combat - package (notes)
- [leetcode] dynamic programming solution split integer i[silver fox]
- [LeetCode]186. Flip word II in string
- Go從入門到實戰——接口(筆記)
- STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
- How to design an elegant caching function
- Let Ma Huateng down! Web3.0, hopeless
- I think I should start writing my own blog.
猜你喜欢

Tiktok's interest in e-commerce has hit the traffic ceiling?

Go from introduction to actual combat -- channel closing and broadcasting (notes)

微服务之远程调用

读写分离-Mysql的主从复制

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

Simulink导出FMU模型文件方法

STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP

关于异常处理的知识整理

Go from introduction to practice - Interface (notes)

MYSQL和MongoDB的分析
随机推荐
win11桌面出現“了解此圖片”如何删除
[LeetCode]186. Flip word II in string
[leetcode] dynamic programming solution split integer i[silver fox]
Go 访问GBase 8a 数据库的一个方法
Go从入门到实战——错误机制(笔记)
[LeetCode]161. Edit distance of 1
TreeSet详解
豆沙绿保护你的双眼
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
【MySQL】数据库函数通关教程下篇(窗口函数专题)
Go from entry to practice - dependency management (notes)
Interval DP of Changyou dynamic programming
[LeetCode]572. A subtree of another tree
[LeetCode]30. 串联所有单词的子串
win11桌面出现“了解此图片”如何删除
I think I should start writing my own blog.
A method of go accessing gbase 8A database
快速excel导出
创建对象时JVM内存结构
Go从入门到实战—— 多路选择和超时控制(笔记)