[R] Help with complicated regular expression

Tony Plate tplate at acm.org
Fri Nov 13 15:27:34 CET 2009


One of these should be a start.  If there can be no extra text at the beginning or end, start with "^" and end with "$".

> x <- c("WORD (  123    )", "WORD(1 )", "WORD\t ( 21\t)", "WORD \t ( 1 \t   )", "decoy((2))", "more words in front(2)")
> grep("[[:alpha:]]+[ \t]*\\([ \t]*[0-9]+[ \t]*\\)", x)
[1] 1 2 3 4 6
> grep("^[[:alpha:]]+[ \t]*\\([ \t]*[0-9]+[ \t]*\\)", x)
[1] 1 2 3 4
> 

-- Tony Plate

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
> 
> ______________________________________________
> 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