当前位置:网站首页>\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)
# [] 匹配不到任何东西
边栏推荐
- 专题教程——选队长游戏
- [LeetCode]161. 相隔为 1 的编辑距离
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- 微服务之远程调用
- [Sword Offer II]剑指 Offer II 029. 排序的循环链表
- [LeetCode]186. Flip word II in string
- Go从入门到实战—— 多路选择和超时控制(笔记)
- [leetcode] 508. Élément de sous - arbre le plus fréquent et
- "Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
- 熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
猜你喜欢

Burp suite遇到的常见问题

VMware virtual machine PE startup

快递e栈——数组篇小型项目

Go从入门到实战——错误机制(笔记)

【MySQL】数据库函数通关教程下篇(窗口函数专题)

Go from introduction to actual combat - package (notes)

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

JVM memory structure when creating objects

I think I should start writing my own blog.

Go從入門到實戰——接口(筆記)
随机推荐
大厂常用软件测试面试题三(附答案)
洛谷P5706 再分肥宅水
Go从入门到实战——多态(笔记)
美团20k软件测试工程师的经验分享
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Let Ma Huateng down! Web3.0, hopeless
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
Go from introduction to practice - Interface (notes)
Example of using gbase 8A OLAP function group by grouping sets
豆沙绿保护你的双眼
QT large file generation MD5 check code
[LeetCode]508. 出現次數最多的子樹元素和
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
GBase 8a OLAP分析函数cume_dist的使用样例
Go从入门到实战——package(笔记)
猜拳游戏专题训练
TreeSet详解
流程控制任务
The create database of gbase 8A takes a long time to query and is suspected to be stuck