[R] regexpr virtue

Chuck Taylor ctaylor at tibco.com
Wed Jul 29 19:45:10 CEST 2009


Petr,

Here is a different approach. It, in effect, works from the end of the
string backwards, rather than from the beginning of the string forwards.

   substring(x, regexpr("[0-9]+$", x))

The regular expression "[0-9]+$" finds a string of 1 or more ("+")
digits ("[0-9]") followed by the end of the line ("$"). The regexpr()
function returns the position in the string x where that regular
expression begins. Feed that to substring() and you get the desired
result. Both substring() and regexpr() work on vectors of strings.

Best regards,
Chuck Taylor
TIBCO Spotfire Seattle


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Petr PIKAL
Sent: Wednesday, July 29, 2009 6:10 AM
To: r-help at stat.math.ethz.ch
Subject: [R] regexpr virtue

Hi all

I have got something like that (actually those are column names)

[51] "X19.2.300b...80"          "X19.2.400v...80" "X19.2.400b...80"  
"X19.2.300v...90"          "X19.2.300b...90" 
[56] "X19.2.400v...90"          "X19.2..400b..90" "X19.2.300v...100"  
"X19.2.300b...100"         "X19.2.400v...100" 

in character vector. I would like to get last n figures from this 
character vector but my regexpr unerstanding is inferior and I did not 
succeed. 
Please, is there anybody who can extract those n digits from end of each

character string? If there were only 2 digits I could use substr but
there 
can be one, two or three digits.

...

Best regards
Petr




More information about the R-help mailing list