[R] Help with complicated regular expression
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Nov 13 15:44:10 CET 2009
\w+ will match one or more word characters and \s* will match 0 or
more spacing characters so if this must the described text must be the
complete expression then:
grepl("^\\w+\\s*\\(\\s*\\w+\\s*\\)$", x)
or if its ok for other text to appear before and after as long as the
indicated text is among it then remove the ^ and $. The above gives a
logical vector as a result or if we use grep rather than grepl we can
get a vector of indexes.
On Fri, Nov 13, 2009 at 9:12 AM, Dennis Fisher <fisher at plessthan.com> 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