Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting regex options in R

Tags:

regex

r

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?

like image 279
Jason V Avatar asked Jan 25 '26 00:01

Jason V


2 Answers

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.)

like image 180
Wiktor Stribiżew Avatar answered Jan 26 '26 13:01

Wiktor Stribiżew


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)
like image 21
hwnd Avatar answered Jan 26 '26 12:01

hwnd