Re: [R] regular expression question

From: Dirk Eddelbuettel <edd_at_debian.org>
Date: Mon 12 Jun 2006 - 07:58:36 EST

On 11 June 2006 at 14:35, Jeff Newmiller wrote:
| >>gsub("(\\d*)$","",c("AAL123", "XELB245", "A247", "FOO123BAR"), perl=TRUE)
| >
| > [1] "AAL" "XELB" "A" "FOO123BAR"
| >
| >
| > gsub finds what is described by the first regexp [ here (\\d\*)$ --- any
| > sequence of digits before the end-of-line ] and applies the second regexp
| > [ here an empty string as we simply delete ] to the third argument.
| >
| > Note
| > - how the $ symbol $ \b prevents it from eating the non-final digits
| > in the counter example FOO123BAR
| > - how the \d for digits needs escaped backslashes \\d
| > - how the * char denotes '1 or more of the preceding thingie'
|
| * normally means "zero or more of the preceding thingie"
| + is the "1 or more or the preceding thingie"
|
| The difference would be apparent if the string being inserted was not
| empty.
|
| > gsub("(\\d*)$","new",c("AAL123", "XELB245", "A247", "FOO123BAR"), perl=TRUE)
| [1] "AALnew" "XELBnew" "Anew" "FOO123BARnew"
|
| > gsub("(\\d+)$","new",c("AAL123", "XELB245", "A247", "FOO123BAR"), perl=TRUE)
| [1] "AALnew" "XELBnew" "Anew" "FOO123BAR"

Thanks for catching, and correcting, that.

Dirk

-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Jun 12 08:18:51 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 12 Jun 2006 - 14:12:50 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.