The documentation for regex, R version 3.1.2, states that
Perl-like matching can work in several modes, set by the options (?i) (caseless, equivalent to Perl's /i), (?m) (multiline, equivalent to Perl's /m), (?s) (single line, so a dot matches all characters, even new lines: equivalent to Perl's /s) and (?x) (extended, whitespace data characters are ignored unless escaped and comments are allowed: equivalent to Perl's /x). These can be concatenated, so for example, (?im) sets caseless multiline matching.
However, I'm unable to use those options, except for (?i) without getting an error:
my.str <- "Abc
abc
ABC"
grep(pattern = "(?mi)abc", x = my.str)
For the placement of the option in the regex, I tried both at the beginning and end with the same results. Any idea how to make options other than (?i) work?
Use perl=TRUE: grep(pattern = "(?mi)abc", x = my.str, perl = TRUE). Have a look at http://astrostatistics.psu.edu/su07/R/html/base/html/grep.html.
The regular expressions used are those specified by POSIX 1003.2, either extended or basic, depending on the value of the extended argument, unless perl = TRUE when they are those of PCRE, http://www.pcre.org/. (The exact set of patterns supported may depend on the version of PCRE installed on the system in use, if R was configured to use the system PCRE. R's internal copy used PCRE 6.7.)
The most advantageous way to use regular expressions with R is to pass the perl=TRUE parameter. This tells R to use the PCRE library. As stated in the R Documentation ...
The perl = TRUE argument to grep, regexpr, gregexpr, sub, gsub and strsplit switches to the PCRE library that implements regular expression pattern matching using the same syntax and semantics as Perl 5.x, with just a few differences.
grep('(?mi)abc', my.str, perl = TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With