# [R] Hep with regex! - combination of ^, |, \\, _ and \$

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Sep 17 23:49:53 CEST 2015

For the last one, looks like this one works:
x[grep("^(q10|q12).*\\_1\$", x)]

On Thu, Sep 17, 2015 at 5:46 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Duncan,
> Of course my verbal descriptions and my code don't match my regexp -
> otherwise I wouldn't be asking the question, would I?
> Please assume my verbal descriptions are correctly describing what I want.
> Thank you!
>
> On Thu, Sep 17, 2015 at 5:42 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com> wrote:
>> On 17/09/2015 5:11 PM, Dimitri Liakhovitski wrote:
>>> (x <- c("q10_1", "q10_2", "q10_11", "q12_1", "q12_2", "q13_1", "q13_11"))
>>>
>>> x[grep("^q10|q12", x)]
>>>
>>> # Which strings end with "1"? - WORKS
>>> x[grep("1\$", x)]
>>>
>>> # Which strings end with "_1"? - WORKS
>>> x[grep("\\_1\$", x)]
>>>
>>> # Which strings start with "q10" AND contain a "1"? - WORKS
>>> x[grep("^q10.+1", x)]
>>>
>>> # Which strings start with "q10" AND end with a "_1"? - DOES NOT WORK
>>> x[grep("^q10.+\\_1\$", x)]
>>
>> Your verbal description doesn't match your regexp, and you didn't show
>> us your output, so how can we tell whether this is you not understanding
>> regular expressions, or an actual problem?
>>
>> When I try this example, I get
>>
>> character(0)
>>
>> which is the correct outcome, given the input string.
>>
>> Duncan Murdoch
>>
>>>
>>> # Which strings start with "q10" or "q12 AND end with "_1"? - WORKS INCORRECTLY
>>> x[grep("^q10|q12.+\\_1\$", x)]
>>>
>>> Thank you!
>>> Dimitri Liakhovitski
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>
>
>
> --
> Dimitri Liakhovitski

--
Dimitri Liakhovitski