Skip to content

R Regular Expressions - Metacharacters

3 messages · Seth Dickey, Duncan Murdoch, David Winsemius

#
On 05/02/2013 12:49 PM, Seth Dickey wrote:
grepl wants a string containing a single backslash.  R uses the 
backslash as an escape character, so you need to double it in your 
source, so the string ends up containing just one.

"\w" is interpreted by R as an escaped w, which doesn't make sense.

"\\w" is interpreted by R as a backslash followed by a w, and then the 
\w is interpreted by grepl the way you want.

Duncan Murdoch
#
On Feb 5, 2013, at 9:49 AM, Seth Dickey wrote:

            
The help page for ?regex says near the top ...

"Any metacharacter with special meaning may be quoted by preceding it with a backslash. The metacharacters in EREs are . \ | ( ) [ { ^ $ * + ?, but note that whether these have a special meaning depends on the context."
David Winsemius
Alameda, CA, USA