r - Matching pattern multiple times in same string with regex -
this question has answer here:
i'm trying find matches of particular pattern "8ab|ab8"
in string "8ab8"
. tried r command gregexpr("8ab|ab8","8ab8")
hoping return vector starting positions c(1,2)
.
unfortunately, seems happens once first pattern matched, portion of string "removed" , second pattern won't matched.
for example, once "8ab" matched, "8ab8" becomes "8" , when r tries matching "ab8" in "8", pattern won't found. know because gregexpr("8ab|ab8","8ab ab8")
works fine , returns starting positions of pattern matches c(1,5)
.
the question is, how match same pattern multiple times in first case?
use perl regular expressions: perl=true . (see ?regex info on perl regular expressions)
gregexpr("(?=8ab)|(?=ab8)","8ab8",perl=t)
Comments
Post a Comment