Does “\d” in regex mean a digit?

前端 未结 6 1193
天命终不由人
天命终不由人 2020-11-22 14:16

I found that in 123, \\d matches 1 and 3 but not 2. I was wondering if \\d matches a digit sati

相关标签:
6条回答
  • 2020-11-22 14:40

    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.

    0 讨论(0)
  • 2020-11-22 14:41

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

    0 讨论(0)
  • 2020-11-22 14:46

    [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 ٠١٢٣٤٥٦٧٨٩.

    0 讨论(0)
  • 2020-11-22 14:50

    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

    0 讨论(0)
  • 2020-11-22 14:51

    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>
    
    0 讨论(0)
  • 2020-11-22 15:00

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

    0 讨论(0)
提交回复
热议问题