Difference between [0-9], [[:digit:]] and \d
Yes, it is [[:digit:]]
~ [0-9]
~ \d
(where ~ means aproximate).
In most programming languages (where it is supported) \d
≡ [[:digit:]]
(identical).
The \d
is less common than [[:digit:]]
(not in POSIX but it is in GNU grep -P
).
There are many digits in UNICODE, for example:
123456789 # Hindu-Arabic
Arabic numerals٠١٢٣٤٥٦٧٨٩ # ARABIC-INDIC
۰۱۲۳۴۵۶۷۸۹ # EXTENDED ARABIC-INDIC/PERSIAN
߀߁߂߃߄߅߆߇߈߉ # NKO DIGIT
०१२३४५६७८९ # DEVANAGARI
All of which may be included in [[:digit:]]
or \d
.
Instead, [0-9]
is generally only the ASCII digits 0123456789
.
There are many languages: Perl, Java, Python, C. In which [[:digit:]]
(and \d
) calls for an extended meaning. For example, this perl code will match all the digits from above:
$ a='0123456789 ٠١٢٣٤٥٦٧٨٩ ۰۱۲۳۴۵۶۷۸۹ ߀߁߂߃߄߅߆߇߈߉ ०१२३४५६७८९'
$ echo "$a" | perl -C -pe 's/[^\d]//g;' ; echo
0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९
Which is equivalent to select all characters that have the Unicode properties of Numeric
and digits
:
$ echo "$a" | perl -C -pe 's/[^\p{Nd}]//g;' ; echo
0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९
Which grep could reproduce (the specific version of pcre may have a diferent internal list of numeric code points than Perl):
$ echo "$a" | grep -oP '\p{Nd}+'
0123456789
٠١٢٣٤٥٦٧٨٩
۰۱۲۳۴۵۶۷۸۹
߀߁߂߃߄߅߆߇߈߉
०१२३४५६७८९
Change it to [0-9] to see:
$ echo "$a" | grep -o '[0-9]\+'
0123456789
POSIX
For the specific POSIX BRE or ERE:
The \d
is not supported (not in POSIX but is in GNU grep -P
). [[:digit:]]
is required by POSIX to correspond to the digit character class, which in turn is required by ISO C to be the characters 0 through 9 and nothing else. So only in C locale all [0-9]
, [0123456789]
, \d
and [[:digit:]]
mean exactly the same. The [0123456789]
has no possible misinterpretations, [[:digit:]]
is available in more utilities and it is common to mean only [0123456789]
. The \d
is supported by few utilities.
As for [0-9]
, the meaning of range expressions is only defined by POSIX in the C locale; in other locales it might be different (might be codepoint order or collation order or something else).
Difference between [0-9], [[:digit:]] and \d的更多相关文章
- 好用到没朋友的大数模板(c++) 2014-10-01 15:06 116人阅读 评论(0) 收藏
#include <iostream> #include <cstring> using namespace std; #define DIGIT 4 //四位隔开,即万进制 ...
- [转]Wing IDE 6.0 安装及算号器注册机代码
下载安装wing 选择第三个,运行算号器,输入license id 输入request id. Python 2 算号器注册机代码 import string import random import ...
- javascript:jQuery tablesorter 2.0
https://mottie.github.io/tablesorter/docs/index.html 1.GridView <%@ Page Language="C#" ...
- WingIDE6.0神秘代码
python2: import string import random import sha BASE16 = '0123456789ABCDEF' BASE30 = '123456789ABCDE ...
- Wing IDE 6.0 算号器注册机代码
我开发Python时喜欢用Wing IDE, 然后最近发现Wing IDE升级到6.0版本了, 但是之前能在5.1上用的算号器代码不能用在6.0上了, 所以就上网搜搜是否有相关算号器, 果然, 找到了 ...
- WingIDE 5.0注冊机
在wingIDE下开发python很方便,但IDE不是免费的,网上有破解的方法.请支持正版. 把下列文件CalcActivationCode.py载入到wingIDE中.LicenseID能够随便给一 ...
- 0.0.0.0 IPAddress.Any 【】127.0.0.1 IPAddress.Loopback 【】localhost
0.0.0.0 IPAddress.Any https://msdn.microsoft.com/en-us/library/system.net.ipaddress.any(v=vs.110).a ...
- 经典面试编程题--atoi()函数的实现(就是模拟手算,核心代码就一句total = 10 * total + (c - '0'); 但是要注意正负号、溢出等问题)
一.功能简介 把一个字符串转换成整数 二.linux c库函数实现 /*** *long atol(char *nptr) - Convert string to long * *Purpose: * ...
- 51 Nod1042 数字0到9的数量
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19 ...
- c语言NULL和0区别及NULL详解
先看下面一段代码输出什么: #include<stdo.h> int main() { int *p=NULL; printf("%s",p); } 输出<n ...
随机推荐
- MySQL自增属性auto_increment_increment和auto_increment_offset
MySQL的系统变量或会话变量auto_increment_increment(自增步长)和auto_increment_offset(自增偏移量)控制着数据表的自增列ID. mysql> sh ...
- BZOJ 1098: [POI2007]办公楼biu 链表
求补图连通块,用链表优化,势能O(n+m) #include<cstdio> #include<cstring> #include<iostream> #inclu ...
- Spring中Resource接口的前缀书写格式
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt"); //这个 ...
- mysql的中文乱码问题
当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1995-10-08','2015-11-12',' ...
- mysql删除id最小的条目
DELETE FROM 表1 WHERE Mid in (select Mid from (SELECT Min(Mid) Mid FROM 表1 c1) t1);
- Spring学习-- AOP入门动态代理
AOP 的拦截功能是由 java 中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常 ...
- Lesson 2
周末重新学习了一下java,有了些新的体会 1.关于jdk, jre,and JVM: Jdk: java development kit,面向开发人员的java开发工具包 Jre:java run ...
- 知问前端——工具提示UI
工具提示(tooltip),是一个非常实用的UI.它彻底扩展了HTML中的title属性,让提示更加丰富,更加可控制,全面提升了用户体验. 调用tooltip()方法 在调用tooltip()方法之前 ...
- php中的split函数
字符串分割函数:split函数 <?php $email='microsoft@exam!ple.com'; $domain = split('\.|@|!',$email);//split分割 ...
- Matlab xpC启动盘
要点: 1.target PC的网卡支持类型有限: 2.网上所列教程未必适用于本地,仅以两图表示: a,带有图形界面时容易出错: b,启动选项为Removable Device: