Re: [R] Escaping regular expressions
Try the fixed argument: grep(.$^, c(Test.$^, Test), fixed = TRUE) On Fri, Nov 13, 2009 at 11:33 AM, Hadley Wickham had...@rice.edu wrote: Hi all, Is there a method for escaping strings to be used regular expressions? i.e. if I have a user supplied string that I'd like to use as a fixed component is there a method that will turn (e.g.) .$^ into \\.\\$\\^ ? Thanks, Hadley -- http://had.co.nz/ __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@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.
Re: [R] Escaping regular expressions
I want the user supplied string to be used as a component of a regular expression that I build up from other pieces that need actual regular expressions. Hadley On Fri, Nov 13, 2009 at 7:41 AM, Henrique Dallazuanna www...@gmail.com wrote: Try the fixed argument: grep(.$^, c(Test.$^, Test), fixed = TRUE) On Fri, Nov 13, 2009 at 11:33 AM, Hadley Wickham had...@rice.edu wrote: Hi all, Is there a method for escaping strings to be used regular expressions? i.e. if I have a user supplied string that I'd like to use as a fixed component is there a method that will turn (e.g.) .$^ into \\.\\$\\^ ? Thanks, Hadley -- http://had.co.nz/ __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- http://had.co.nz/ __ R-help@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.
Re: [R] Escaping regular expressions
This is not minimal (it will escape every non-word character) but is short and the needlessly escaped ones should do no harm (though you might want to double check that assumption on a few examples): gsub((\\W), \\1, x) On Fri, Nov 13, 2009 at 8:33 AM, Hadley Wickham had...@rice.edu wrote: Hi all, Is there a method for escaping strings to be used regular expressions? i.e. if I have a user supplied string that I'd like to use as a fixed component is there a method that will turn (e.g.) .$^ into \\.\\$\\^ ? Thanks, Hadley -- http://had.co.nz/ __ R-help@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. __ R-help@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.
Re: [R] Escaping regular expressions
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Hadley Wickham Sent: Friday, November 13, 2009 5:34 AM To: r-help Subject: [R] Escaping regular expressions Hi all, Is there a method for escaping strings to be used regular expressions? i.e. if I have a user supplied string that I'd like to use as a fixed component is there a method that will turn (e.g.) .$^ into \\.\\$\\^ ? I've used the following: asFixedRegex - function(pattern) { gsub(([][^${}().?*+\\]), \\1, pattern) } E.g., asFixedRegex(.$^) [1] \\.\\$\\^ or x - c(C:\\Program Files\\word, P[[:alnum:]]{2,3}, (x+y)[1*3]) regexpr(paste(collapse=|, asFixedRegex(x)), x) [1] 1 1 1 attr(,match.length) [1] 21 17 10 nchar(x) # expect same as match.length [1] 21 17 10 Is there a standard function to do this? If so, I'd like to know its name. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Thanks, Hadley -- http://had.co.nz/ __ R-help@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. __ R-help@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.