Invalid regex in R

I am trying to use this regexp in R:

\?(?=([^'\\]*(\\.|'([^'\\]*\\.)*[^'\\]*'))*[^']*$)

I run away like this:

\\?(?=([^'\\\\]*(\\\\.|'([^'\\\\]*\\\\.)*[^'\\\\]*'))*[^']*$)

I am getting error invalid regexp

.

Regexpal has no regex problem and I have verified that the interpreted regex in the R error message is the same as what I use in Buddy Regex, so I kind of don't get it. I don't think the problem is the problem.

code:

output <- sub("\\?(?=([^'\\\\]*(\\\\.|'([^'\\\\]*\\\\.)*[^'\\\\]*'))*[^']*$)", "!", "This is a test string?")

      

+3


source to share


1 answer


R

by default uses POSIX

( P ortable O referring to S ystem I nterface) standard regular expressions (see these SO posts [ 1 , 2 ] and ?regex

[caveat emptor: ahead of machete level density]).

-Ahead Look ( (?=...)

) look-behind ( (?<=...)

) and denying them ( (?!...)

and (?<!...)

) are probably the most striking examples PCRE

-specific ( P erl- C ompatible R egular E xpressions), which are not compatible with POSIX

.



R

you can train to understand your regular expression by activating the parameter perl

on TRUE

; This option is available in all the regular expression functions base

( gsub

, grepl

, regmatches

etc.):

output <- sub("\\?(?=([^'\\\\]*(\\\\.|'([^'\\\\]*\\\\.)*[^'\\\\]*'))*[^']*$)",
              "!", "This is a test string?", perl = TRUE)

      

+4


source







All Articles