Dimitri Liakhovitski
2015-Sep-17 21:11 UTC
[R] Hep with regex! - combination of ^, |, \\, _ and $
(x <- c("q10_1", "q10_2", "q10_11", "q12_1", "q12_2", "q13_1", "q13_11")) # Which strings start with "q10" or "q12? - WORKS 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)] # Which strings start with "q10" or "q12 AND end with "_1"? - WORKS INCORRECTLY x[grep("^q10|q12.+\\_1$", x)] Thank you! Dimitri Liakhovitski
Duncan Murdoch
2015-Sep-17 21:42 UTC
[R] Hep with regex! - combination of ^, |, \\, _ and $
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")) > > # Which strings start with "q10" or "q12? - WORKS > 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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Dimitri Liakhovitski
2015-Sep-17 21:46 UTC
[R] Hep with regex! - combination of ^, |, \\, _ and $
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")) >> >> # Which strings start with "q10" or "q12? - WORKS >> 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 >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >-- Dimitri Liakhovitski
David Winsemius
2015-Sep-17 22:08 UTC
[R] Hep with regex! - combination of ^, |, \\, _ and $
On Sep 17, 2015, at 2:11 PM, Dimitri Liakhovitski wrote:> (x <- c("q10_1", "q10_2", "q10_11", "q12_1", "q12_2", "q13_1", "q13_11")) > > # Which strings start with "q10" or "q12? - WORKS > 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)] > > # Which strings start with "q10" or "q12 AND end with "_1"? - WORKS INCORRECTLY > x[grep("^q10|q12.+\\_1$", x)] >You solved the last one with grouping around the OR-operator. The penultimate error needs only a very slight change to find a single instance in x: x[grep("^q10.*_1$", x)] [1] "q10_1" Using "+" requires at leas one character between the two end, whereas you use the "*" operator to allow for the possibility of no intervening characters. -- David Winsemius Alameda, CA, USA
Berend Hasselman
2015-Sep-18 05:30 UTC
[R] Hep with regex! - combination of ^, |, \\, _ and $
> On 17 Sep 2015, at 23:11, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: > > (x <- c("q10_1", "q10_2", "q10_11", "q12_1", "q12_2", "q13_1", "q13_11")) > > # Which strings start with "q10" or "q12? - WORKS > 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)] >For these last to this should ?work?> # Which strings start with "q10" AND end with a "_1"? - DOES NOT WORK > x[grep("^q10.+\\_1$", x)] >x[grep("^q10.*\\_1$", x)]> # Which strings start with "q10" or "q12 AND end with "_1"? - WORKS INCORRECTLY > x[grep("^q10|q12.+\\_1$", x)] >x[grep("^q10|q12.*\\_1$", x)] It?s the .+ that?s the problem. Berend> 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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Upton, Stephen (Steve) (CIV)
2015-Sep-18 12:48 UTC
[R] Hep with regex! - combination of ^, |, \\, _ and $
And unless I'm mistaken, escaping the underscore is superfluous (I'd be curious to know if it's a function of locale). x[grep("_", x)] x[grep("^q10.*_1$", x)] both work. steve Stephen C. Upton SEED (Simulation Experiments & Efficient Designs) Center Operations Research Department Naval Postgraduate School SEED Center web site: http://harvest.nps.edu -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Berend Hasselman Sent: Friday, September 18, 2015 1:30 AM To: Dimitri Liakhovitski Cc: r-help Subject: Re: [R] Hep with regex! - combination of ^, |, \\, _ and $> On 17 Sep 2015, at 23:11, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: > > (x <- c("q10_1", "q10_2", "q10_11", "q12_1", "q12_2", "q13_1", > "q13_11")) > > # Which strings start with "q10" or "q12? - WORKS 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)] >For these last to this should ?work?> # Which strings start with "q10" AND end with a "_1"? - DOES NOT WORK > x[grep("^q10.+\\_1$", x)] >x[grep("^q10.*\\_1$", x)]> # Which strings start with "q10" or "q12 AND end with "_1"? - WORKS > INCORRECTLY x[grep("^q10|q12.+\\_1$", x)] >x[grep("^q10|q12.*\\_1$", x)] It?s the .+ that?s the problem. Berend> 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 > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.