I’m trying to get the location of a white space inside a string but I don’t understand the results.
Given the string:
a = “12345,1300 miles”
> gregexpr("\\s", a)
[[1]]
[1] 11
attr(,"match.length")
[1] 1
This makes sense b/c the white space is in index 11 of the string.
> gregexpr("[\\s]", a)
[[1]]
[1] 16
attr(,"match.length")
[1] 1
This does not make sense to me b/c index 16 is simply the end of the string. There is no white space there, and I’m wondering why it skipped index 11.
I’m stumped, can anyone give an explanation on why this is happening?
> gregexpr("\\s*", a)
[[1]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
attr(,"match.length")
[1] 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
This also does not make sense to me b/c the white space matched every single character in the string.
Inside character classes you should probably not be using escaped regex sequences. They are not recognized properly. I do not know if this is proper regex behavior, but there is a sentence in the
?regexpage saying: “Most metacharacters lose their special meaning inside a character class. ” I can successfully use[:space:]insteadIn the second instance it is true that all of those substrings will match that pattern. The behavior of this code is perhaps what you expected:
Or: