Reputation: 2231
This simple regular expression for matching each single digit
gregexpr('[[:digit:]]', 'a1b2c3')
returns what I expect:
[[1]]
[1] 2 4 6
attr(,"match.length")
[1] 1 1 1
attr(,"useBytes")
[1] TRUE
But the R documentation suggests a more compact syntax:
Symbols \d, \s, \D and \S denote the digit and space classes and their negations (these are all extensions).
So I tried
gregexpr('[\d]', 'a1b2c3')
but this returns an error:
Error: '\d' is an unrecognized escape in character string starting "'[\d"
How is this "extension" used?
Upvotes: 1
Views: 668
Reputation: 3875
The equivalent of
gregexpr('[[:digit:]]', 'a1b2c3')
is
gregexpr('\\d', 'a1b2c3')
You need to precede the regex character class by a double backslash (\\
).
Upvotes: 2