[R] strsplit with invalid regular expression
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Thu Nov 8 17:17:39 CET 2012
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:
>Hi all,
>
>> diff_operator <- "\\("
>> strsplit(cond, diff_operator)
>[[1]]
>[1] "andsin" "log_angle_1_4)"
>
>> diff_operator <- "\\sin("
>> strsplit(cond, diff_operator)
>Error in strsplit(cond, diff_operator) :
> invalid regular expression '\sin(', reason 'Missing ')''
>
>When I am going to split with "(" it’s working fine but when i want use
>"sin(" this is not working.
>I understand that, I am missing something can anybody please point me.
>Thanks for your help.
>
>Thanks,
>Bharat
>
>
>
>-----
>Bharat Warule
>Pune
>--
>View this message in context:
>http://r.789695.n4.nabble.com/strsplit-with-invalid-regular-expression-tp4648857.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list