grep for multiple pattern?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

grep for multiple pattern?

Rainer Krug-3
Hi

I want to search for multiple pattern as grep is doing for a single
pattern, but this obviously not work:

> grep("an", month.name)
[1] 1
> grep("em", month.name)
[1]  9 11 12
> grep("eb", month.name)
[1] 2
> grep(c("an", "em", "eb"), month.name)
[1] 1
Warning message:
In grep(c("an", "em", "eb"), month.name) :
  argument 'pattern' has length > 1 and only the first element will be used
>

Is there an equivalent which returns the positions as grep is doing, but
not using the strict full-string matching of match()?

I could obviously do:

> unlist( sapply(pat, grep, month.name ) )
 an em1 em2 em3  eb
  1   9  11  12   2

but is there a more compact command I am missing?

Thanks,

Rainer

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      [hidden email]

Skype:      RMkrug


______________________________________________
[hidden email] 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.

signature.asc (572 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: grep for multiple pattern?

PIKAL Petr
Hi

Maybe I am missing something but isn't  this

which(letters %in% c("a", "x"))

what you want?

Petr

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Rainer M Krug
> Sent: Thursday, February 13, 2014 3:43 PM
> To: [hidden email]
> Subject: [R] grep for multiple pattern?
>
> Hi
>
> I want to search for multiple pattern as grep is doing for a single
> pattern, but this obviously not work:
>
> > grep("an", month.name)
> [1] 1
> > grep("em", month.name)
> [1]  9 11 12
> > grep("eb", month.name)
> [1] 2
> > grep(c("an", "em", "eb"), month.name)
> [1] 1
> Warning message:
> In grep(c("an", "em", "eb"), month.name) :
>   argument 'pattern' has length > 1 and only the first element will be
> used
> >
>
> Is there an equivalent which returns the positions as grep is doing,
> but not using the strict full-string matching of match()?
>
> I could obviously do:
>
> > unlist( sapply(pat, grep, month.name ) )
>  an em1 em2 em3  eb
>   1   9  11  12   2
>
> but is there a more compact command I am missing?
>
> Thanks,
>
> Rainer
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology, UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology Stellenbosch University South
> Africa
>
> Tel :       +33 - (0)9 53 10 27 44
> Cell:       +33 - (0)6 85 62 59 98
> Fax :       +33 - (0)9 58 10 27 44
>
> Fax (D):    +49 - (0)3 21 21 25 22 44
>
> email:      [hidden email]
>
> Skype:      RMkrug


________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for multiple pattern?

Marc Schwartz-3
In reply to this post by Rainer Krug-3

On Feb 13, 2014, at 8:43 AM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> I want to search for multiple pattern as grep is doing for a single
> pattern, but this obviously not work:
>
>> grep("an", month.name)
> [1] 1
>> grep("em", month.name)
> [1]  9 11 12
>> grep("eb", month.name)
> [1] 2
>> grep(c("an", "em", "eb"), month.name)
> [1] 1
> Warning message:
> In grep(c("an", "em", "eb"), month.name) :
>  argument 'pattern' has length > 1 and only the first element will be used
>>
>
> Is there an equivalent which returns the positions as grep is doing, but
> not using the strict full-string matching of match()?
>
> I could obviously do:
>
>> unlist( sapply(pat, grep, month.name ) )
> an em1 em2 em3  eb
>  1   9  11  12   2
>
> but is there a more compact command I am missing?
>
> Thanks,
>
> Rainer


The vertical bar '|' acts as a logical 'or' operator in regex expressions:

> grep("an|em|eb", month.name)
[1]  1  2  9 11 12

> grep("an|em|eb", month.name, value = TRUE)
[1] "January"   "February"  "September" "November"  "December"


Regards,

Marc Schwartz

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for multiple pattern?

jholtman
In reply to this post by Rainer Krug-3
use the "|" in regular expressions:

 grep(c("an|em|eb", month.name)


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Thu, Feb 13, 2014 at 9:43 AM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> I want to search for multiple pattern as grep is doing for a single
> pattern, but this obviously not work:
>
> > grep("an", month.name)
> [1] 1
> > grep("em", month.name)
> [1]  9 11 12
> > grep("eb", month.name)
> [1] 2
> > grep(c("an", "em", "eb"), month.name)
> [1] 1
> Warning message:
> In grep(c("an", "em", "eb"), month.name) :
>   argument 'pattern' has length > 1 and only the first element will be used
> >
>
> Is there an equivalent which returns the positions as grep is doing, but
> not using the strict full-string matching of match()?
>
> I could obviously do:
>
> > unlist( sapply(pat, grep, month.name ) )
>  an em1 em2 em3  eb
>   1   9  11  12   2
>
> but is there a more compact command I am missing?
>
> Thanks,
>
> Rainer
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology, UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Stellenbosch University
> South Africa
>
> Tel :       +33 - (0)9 53 10 27 44
> Cell:       +33 - (0)6 85 62 59 98
> Fax :       +33 - (0)9 58 10 27 44
>
> Fax (D):    +49 - (0)3 21 21 25 22 44
>
> email:      [hidden email]
>
> Skype:      RMkrug
>
>
> ______________________________________________
> [hidden email] 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for multiple pattern?

Rainer Krug-3

On 02/13/14, 17:23 , jim holtman wrote:
> use the "|" in regular expressions:
>
>  grep(c("an|em|eb", month.name <http://month.name/>)

Thanks - again a reason to learn regexp.

Cheers,

Rainer

>
>
> Jim Holtman
> Data Munger Guru
>  
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
>
> On Thu, Feb 13, 2014 at 9:43 AM, Rainer M Krug <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi
>
>     I want to search for multiple pattern as grep is doing for a single
>     pattern, but this obviously not work:
>
>     > grep("an", month.name <http://month.name>)
>     [1] 1
>     > grep("em", month.name <http://month.name>)
>     [1]  9 11 12
>     > grep("eb", month.name <http://month.name>)
>     [1] 2
>     > grep(c("an", "em", "eb"), month.name <http://month.name>)
>     [1] 1
>     Warning message:
>     In grep(c("an", "em", "eb"), month.name <http://month.name>) :
>       argument 'pattern' has length > 1 and only the first element will
>     be used
>     >
>
>     Is there an equivalent which returns the positions as grep is doing, but
>     not using the strict full-string matching of match()?
>
>     I could obviously do:
>
>     > unlist( sapply(pat, grep, month.name <http://month.name> ) )
>      an em1 em2 em3  eb
>       1   9  11  12   2
>
>     but is there a more compact command I am missing?
>
>     Thanks,
>
>     Rainer
>
>     --
>     Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
>     Biology, UCT), Dipl. Phys. (Germany)
>
>     Centre of Excellence for Invasion Biology
>     Stellenbosch University
>     South Africa
>
>     Tel :       +33 - (0)9 53 10 27 44
>     <tel:%2B33%20-%20%280%299%2053%2010%2027%2044>
>     Cell:       +33 - (0)6 85 62 59 98
>     <tel:%2B33%20-%20%280%296%2085%2062%2059%2098>
>     Fax :       +33 - (0)9 58 10 27 44
>     <tel:%2B33%20-%20%280%299%2058%2010%2027%2044>
>
>     Fax (D):    +49 - (0)3 21 21 25 22 44
>     <tel:%2B49%20-%20%280%293%2021%2021%2025%2022%2044>
>
>     email:      [hidden email] <mailto:[hidden email]>
>
>     Skype:      RMkrug
>
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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.
>
>
--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      [hidden email]

Skype:      RMkrug


______________________________________________
[hidden email] 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.

signature.asc (572 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: grep for multiple pattern?

Keith Jewell-2
In reply to this post by Marc Schwartz-3
On 13/02/2014 15:51, Marc Schwartz wrote:

>
> On Feb 13, 2014, at 8:43 AM, Rainer M Krug <[hidden email]> wrote:
>
>> Hi
>>
>> I want to search for multiple pattern as grep is doing for a single
>> pattern, but this obviously not work:
>>
>>> grep("an", month.name)
>> [1] 1
>>> grep("em", month.name)
>> [1]  9 11 12
>>> grep("eb", month.name)
>> [1] 2
>>> grep(c("an", "em", "eb"), month.name)
>> [1] 1
>> Warning message:
>> In grep(c("an", "em", "eb"), month.name) :
>>   argument 'pattern' has length > 1 and only the first element will be used
>>>
>>
>> Is there an equivalent which returns the positions as grep is doing, but
>> not using the strict full-string matching of match()?
>>
>> I could obviously do:
>>
>>> unlist( sapply(pat, grep, month.name ) )
>> an em1 em2 em3  eb
>>   1   9  11  12   2
>>
>> but is there a more compact command I am missing?
>>
>> Thanks,
>>
>> Rainer
>
>
> The vertical bar '|' acts as a logical 'or' operator in regex expressions:
>
>> grep("an|em|eb", month.name)
> [1]  1  2  9 11 12
>
>> grep("an|em|eb", month.name, value = TRUE)
> [1] "January"   "February"  "September" "November"  "December"
>
>
> Regards,
>
> Marc Schwartz
>
and if you want your patterns in a vector
 > pat <-c("an", "em", "eb")
 > grep(paste(pat, collapse="|"), month.name)
[1]  1  2  9 11 12

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for multiple pattern?

Prof Brian Ripley
In reply to this post by Rainer Krug-3
On 13/02/2014 16:25, Rainer M Krug wrote:
>
> On 02/13/14, 17:23 , jim holtman wrote:
>> use the "|" in regular expressions:
>>
>>   grep(c("an|em|eb", month.name <http://month.name/>)
>
> Thanks - again a reason to learn regexp.

Note though that is an *extended* regex.  They are the default in R, but
not for grep, sed, ....

Another thing to watch out is that GNU grep allows (a\|b\|c) in 'basic'
regexps -- but the POSIX standard does not, and nor do other
implementations.  The authors of the graphviz configure code (shipped
with Rgraphviz) did not know this and wasted other people's resources.

>
> Cheers,
>
> Rainer
>
>>
>>
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>> Tell me what you want to do, not how you want to do it.
>>
>>
>> On Thu, Feb 13, 2014 at 9:43 AM, Rainer M Krug <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>      Hi
>>
>>      I want to search for multiple pattern as grep is doing for a single
>>      pattern, but this obviously not work:
>>
>>      > grep("an", month.name <http://month.name>)
>>      [1] 1
>>      > grep("em", month.name <http://month.name>)
>>      [1]  9 11 12
>>      > grep("eb", month.name <http://month.name>)
>>      [1] 2
>>      > grep(c("an", "em", "eb"), month.name <http://month.name>)
>>      [1] 1
>>      Warning message:
>>      In grep(c("an", "em", "eb"), month.name <http://month.name>) :
>>        argument 'pattern' has length > 1 and only the first element will
>>      be used
>>      >
>>
>>      Is there an equivalent which returns the positions as grep is doing, but
>>      not using the strict full-string matching of match()?
>>
>>      I could obviously do:
>>
>>      > unlist( sapply(pat, grep, month.name <http://month.name> ) )
>>       an em1 em2 em3  eb
>>        1   9  11  12   2
>>
>>      but is there a more compact command I am missing?
>>
>>      Thanks,
>>
>>      Rainer
>>
>>      --
>>      Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
>>      Biology, UCT), Dipl. Phys. (Germany)
>>
>>      Centre of Excellence for Invasion Biology
>>      Stellenbosch University
>>      South Africa
>>
>>      Tel :       +33 - (0)9 53 10 27 44
>>      <tel:%2B33%20-%20%280%299%2053%2010%2027%2044>
>>      Cell:       +33 - (0)6 85 62 59 98
>>      <tel:%2B33%20-%20%280%296%2085%2062%2059%2098>
>>      Fax :       +33 - (0)9 58 10 27 44
>>      <tel:%2B33%20-%20%280%299%2058%2010%2027%2044>
>>
>>      Fax (D):    +49 - (0)3 21 21 25 22 44
>>      <tel:%2B49%20-%20%280%293%2021%2021%2025%2022%2044>
>>
>>      email:      [hidden email] <mailto:[hidden email]>
>>
>>      Skype:      RMkrug
>>
>>
>>      ______________________________________________
>>      [hidden email] <mailto:[hidden email]> 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.
>>
>>
>
>
>
> ______________________________________________
> [hidden email] 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.
>


--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] 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.