[R] Help with complicated regular expression

Romain Francois romain.francois at dbmail.com
Fri Nov 13 15:24:51 CET 2009


Hello,

The function you are looking for is grepl. Something like this perhaps:

 > words <- c("WORD (  123    )","WORD(1)", "WORD\t ( 21\t) ", "WORD\t ( 
21\t) " )
 > grepl( "[[:space:]]*[(][[:space:]]*[0-9]+[[:space:]]*[)]", words )
[1] TRUE TRUE TRUE TRUE

[[:space:]]*     : any number of spaces or tabs (including 0 times)
[(]              : a (
[0-9]+           : any number of digits, but at least one
[)]              : a )

Romain


On 11/13/2009 03:12 PM, Dennis Fisher wrote:
>
> Colleagues,
>
> I am using R (2.9.2, all platforms) to search for a complicated text
> string using regular expressions. I would appreciate any help you can
> provide.
> The string consists of the following elements:
> SOMEWORDWITHNOSPACES
> any number of spaces and/or tabs
> (
> any number of spaces and/or tabs
> integer
> any number of spaces and/or tabs
> )
>
> Examples include:
> WORD ( 123 )
> WORD(1 )
> WORD\t ( 21\t)
> WORD \t ( 1 \t )
> etc.
>
> I don't need to substitute anything, only to identify if such a string
> exists.
> Any help with regular expressions would be appreciated.
> Thanks.
>
> Dennis
>
>
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/EAD5 : LondonR slides
|- http://tr.im/BcPw : celebrating R commit #50000
`- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc




More information about the R-help mailing list