Skip to content
Prev 310270 / 398503 Next

strsplit with invalid regular expression

In R, the source code representation of a special character uses the \ as an escape character to begin a special character sequence. For example, "\n" is a single newline character.

Because backslash has this special meaning, to represent a single backslash character one must escape it: "\\" looks like two backslashes, but in computer memory it is only one character.

R is not the only software that uses the backslash as an escape character: the syntax of regular expressions handled by the regex library also does, but it uses the backslash to SUPPRESS special meaning.

In your first regex you pass a string containing a backslash followed by a left paren, which tells it to treat the ( as a character to be searched for, not a grouping character that defines a substring to "remember" for backreferencing.

In your second regex you tell it that the letter s should be treated as a normal character (which it is anyway), but the ( retains its special meaning that requires a matching ) in the regex.

I suspect that what you wanted was "sin\\(", which gives sin\( to the regex library.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Bharat Warule <bwarule at gmail.com> wrote: