标签归档:gedit

正则表达式中的“ \ d”表示数字吗?

问题:正则表达式中的“ \ d”表示数字吗?

我发现123\d比赛13,但不会2。我想知道\d匹配的数字是否满足哪种要求?我说的是Python样式的正则表达式。

Gedit中的正则表达式插件使用Python样式正则表达式。我创建了一个文本文件,其内容为

123

正则表达式只13匹配\d2不是。

通常,对于一系列数字,中间没有其他字符,只有奇数位是匹配的,偶数位不是。对于例如12345,比赛是135

I found that in 123, \d matches 1 and 3 but not 2. I was wondering if \d matches a digit satisfying what kind of requirement? I am talking about Python style regex.

Regular expression plugin in Gedit is using Python style regex. I created a text file with its content being

123

Only 1 and 3 are matched by the regex \d; 2 is not.

Generally for a sequence of digit numbers without other characters in between, only the odd order digits are matches, and the even order digits are not. For example in 12345, the matches are 1, 3 and 5.


回答 0

[0-9] 并不总是等同\d。在python3中,[0-9]仅匹配0123456789字符,而\d匹配[0-9]其他字符,例如东部阿拉伯数字٠١٢٣٤٥٦٧٨٩

[0-9] is not always equivalent to \d. In python3, [0-9] matches only 0123456789 characters, while \d matches [0-9] and other digit characters, for example Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩.


回答 1

\d匹配大多数正则表达式语法样式中的任何一位,包括python。 正则表达式参考

\d matches any single digit in most regex grammar styles, including python. Regex Reference


回答 2

在Python样式的正则表达式中,\d匹配任何单个数字。如果您看到的东西似乎无法做到这一点,请提供您正在使用的完整正则表达式,而不是仅仅描述一个特定的符号。

>>> import re
>>> re.match(r'\d', '3')
<_sre.SRE_Match object at 0x02155B80>
>>> re.match(r'\d', '2')
<_sre.SRE_Match object at 0x02155BB8>
>>> re.match(r'\d', '1')
<_sre.SRE_Match object at 0x02155B80>

In Python-style regex, \d matches any individual digit. If you’re seeing something that doesn’t seem to do that, please provide the full regex you’re using, as opposed to just describing that one particular symbol.

>>> import re
>>> re.match(r'\d', '3')
<_sre.SRE_Match object at 0x02155B80>
>>> re.match(r'\d', '2')
<_sre.SRE_Match object at 0x02155BB8>
>>> re.match(r'\d', '1')
<_sre.SRE_Match object at 0x02155B80>

回答 3

\\d{3} 匹配Java中任意三位数字的序列。

\\d{3} matches any sequence of three digits in Java.


回答 4

这只是一个猜测,但我认为您的编辑器实际上会匹配每个数字,1 2 3但是会突出显示奇数匹配,以区别于整个123字符串都匹配的情况。

大多数正则表达式控制台使用不同的颜色突出显示连续的匹配项,但是由于插件设置,终端限制或其他原因,在您的情况下,可能仅突出显示每个其他组。

This is just a guess, but I think your editor actually matches every single digit — 1 2 3 — but only odd matches are highlighted, to distinguish it from the case when the whole 123 string is matched.

Most regex consoles highlight contiguous matches with different colors, but due to the plugin settings, terminal limitations or for some other reason, only every other group might be highlighted in your case.


回答 5

有关.NET / C#的信息:

小数位字符:\ d \ d匹配任何小数位。它等效于\ p {Nd}正则表达式模式,其中包括标准的十进制数字0-9和许多其他字符集的十进制数字。

如果指定了ECMAScript兼容行为,则\ d等效于[0-9]。有关ECMAScript正则表达式的信息,请参阅正则表达式选项中的“ ECMAScript匹配行为”部分。

信息:https : //docs.microsoft.com/zh-cn/dotnet/standard/base-types/character-classes-in-regular-expressions#decimal-digit-character-d

Info regarding .NET / C#:

Decimal digit character: \d \d matches any decimal digit. It is equivalent to the \p{Nd} regular expression pattern, which includes the standard decimal digits 0-9 as well as the decimal digits of a number of other character sets.

If ECMAScript-compliant behavior is specified, \d is equivalent to [0-9]. For information on ECMAScript regular expressions, see the “ECMAScript Matching Behavior” section in Regular Expression Options.

Info: https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#decimal-digit-character-d