Dear all as usual I am again lost in virtues of regular expressions. I have such character vector named vzor: [365] "61A" "62C/27" "65A/27" "66C/29" "69A/29" "70C/31" "73A/31" "74C/33" "77A/33" "81A/35" "82C/37" "85A/37" "86C/39" [378] "89A/39" "90C/41" "93A/41" "94C/43" "97A/43" "98C/45" "101A/45" "102C/47" "105A/47" "106C/49" "109A/49" "110C/51" "113A/51" and I want only letters from it. I tried> gsub("[[:alpha:]]"," \\1",vzor)Error in gsub("[[:alpha:]]", " \\1", vzor) : invalid backreference 1 in regular expression gsub("[:alpha:]"," \\1",vzor) gives me the same vector There is probably very simple solution to it which I overlooked and examples in help page did not help me to find it. Thank you Best regards Petr Pikal petr.pikal at precheza.cz
gsub("^.*([[:alpha:]]).*$", "\\1", vzor) ..............................................<?}))><........ ) ) ) ) ) ( ( ( ( ( Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( ( Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Belgium ( ( ( ( ( .............................................................. Petr PIKAL wrote:> Dear all > > as usual I am again lost in virtues of regular expressions. > > I have such character vector named vzor: > > [365] "61A" "62C/27" "65A/27" "66C/29" "69A/29" "70C/31" > "73A/31" "74C/33" "77A/33" "81A/35" "82C/37" "85A/37" "86C/39" > > [378] "89A/39" "90C/41" "93A/41" "94C/43" "97A/43" "98C/45" > "101A/45" "102C/47" "105A/47" "106C/49" "109A/49" "110C/51" "113A/51" > > > and I want only letters from it. > > I tried > >> gsub("[[:alpha:]]"," \\1",vzor) > Error in gsub("[[:alpha:]]", " \\1", vzor) : > invalid backreference 1 in regular expression > > gsub("[:alpha:]"," \\1",vzor) > > gives me the same vector > > There is probably very simple solution to it which I overlooked and > examples in help page did not help me to find it. > > Thank you > Best regards > > Petr Pikal > petr.pikal at precheza.cz > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
A backreference is contained in parentheses and there are no parentheses in your regular expression, hence the error message. Its probably easiest just to remove all non-letters: x <- "45x53yy66" gsub("[^[:alpha:]]", "", x) # xyy On 4/18/07, Petr PIKAL <petr.pikal at precheza.cz> wrote:> Dear all > > as usual I am again lost in virtues of regular expressions. > > I have such character vector named vzor: > > [365] "61A" "62C/27" "65A/27" "66C/29" "69A/29" "70C/31" > "73A/31" "74C/33" "77A/33" "81A/35" "82C/37" "85A/37" "86C/39" > > [378] "89A/39" "90C/41" "93A/41" "94C/43" "97A/43" "98C/45" > "101A/45" "102C/47" "105A/47" "106C/49" "109A/49" "110C/51" "113A/51" > > > and I want only letters from it. > > I tried > > > gsub("[[:alpha:]]"," \\1",vzor) > Error in gsub("[[:alpha:]]", " \\1", vzor) : > invalid backreference 1 in regular expression > > gsub("[:alpha:]"," \\1",vzor) > > gives me the same vector > > There is probably very simple solution to it which I overlooked and > examples in help page did not help me to find it. > > Thank you > Best regards > > Petr Pikal > petr.pikal at precheza.cz > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Thank you all for your working solutions Petr Pikal petr.pikal at precheza.cz r-help-bounces at stat.math.ethz.ch napsal dne 18.04.2007 13:26:36:> A backreference is contained in parentheses and there are no parentheses > in your regular expression, hence the error message. > > Its probably easiest just to remove all non-letters: > > x <- "45x53yy66" > gsub("[^[:alpha:]]", "", x) # xyy > > On 4/18/07, Petr PIKAL <petr.pikal at precheza.cz> wrote: > > Dear all > > > > as usual I am again lost in virtues of regular expressions. > > > > I have such character vector named vzor: > > > > [365] "61A" "62C/27" "65A/27" "66C/29" "69A/29" "70C/31" > > "73A/31" "74C/33" "77A/33" "81A/35" "82C/37" "85A/37""86C/39"> > > > [378] "89A/39" "90C/41" "93A/41" "94C/43" "97A/43" "98C/45" > > "101A/45" "102C/47" "105A/47" "106C/49" "109A/49" "110C/51""113A/51"> > > > > > and I want only letters from it. > > > > I tried > > > > > gsub("[[:alpha:]]"," \\1",vzor) > > Error in gsub("[[:alpha:]]", " \\1", vzor) : > > invalid backreference 1 in regular expression > > > > gsub("[:alpha:]"," \\1",vzor) > > > > gives me the same vector > > > > There is probably very simple solution to it which I overlooked and > > examples in help page did not help me to find it. > > > > Thank you > > Best regards > > > > Petr Pikal > > petr.pikal at precheza.cz > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.