Re: [R] A question on regular expression
Regular expressions are in much more widespread use than merely R... and there are correspondingly more resources for learning than just R-help. Please do make use of them. Here are a couple that googling "regex character set carat" found: https://www.regular-expressions.info/charclass.html https://stackoverflow.com/questions/23352038/regex-excluding-specific-characters On September 13, 2019 3:59:07 AM PDT, Christofer Bogaso wrote: >A quick question. > >Could you please explain the -- [^}]* -- part in finding the pattern? > >On Fri, Sep 13, 2019 at 12:19 AM Bert Gunter >wrote: >> >> >> You can't use the same regex for str_extract_all as I used for sub >(or gsub, which is what is required here)! If you do this sort of thing >a lot, you *must* learn more about regex's. >> >> Anyway, this will do what you want I think: >> >> z <- paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " ") ## just for >readability >> >> > str_extract_all(z,"\\{[^}]*\\}") >> [[1]] >> [1] "{cd$ }" "{cad$ }" >> >> Cheers, >> Bert >> >> On Thu, Sep 12, 2019 at 10:12 AM Christofer Bogaso > wrote: >>> >>> Thanks Bert, >>> >>> This works, but if in my text there are more than one patterns then >>> fails to generate desired result. >>> >>> library(stringr) >>> str_extract_all(paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " "), >>> ".*(\\{.*\\}).*") >>> >>> This generates below - >>> >>> [[1]] >>> >>> [1] "ab{cd$ }ed ab{cad$ }ed" >>> >>> I was expecting I would get a vector of length 2 with desired >pattern. >>> >>> Where did I make any mistake? >>> >>> Thanks, >>> >>> On Thu, Sep 12, 2019 at 10:29 PM Bert Gunter > wrote: >>> > >>> > > sub(".*(\\{.*\\}).*", "\\1","ab{cd$ }ed") >>> > [1] "{cd$ }" >>> > >>> > Use ".+" instead of ".*" within the {} if you don't want to return >empty {}'s. >>> > >>> > You might wish to use the stringr package for string matching and >manipulation, as it provides a more user friendly and consistent >interface to these tasks. >>> > >>> > >>> > Bert Gunter >>> > >>> > "The trouble with having an open mind is that people keep coming >along and sticking things into it." >>> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >>> > >>> > >>> > On Thu, Sep 12, 2019 at 9:31 AM Christofer Bogaso > wrote: >>> >> >>> >> Hi, >>> >> >>> >> I am wondering on what is the correct way to select a pattern >which goes as - >>> >> >>> >> {"(any character with any length)"} >>> >> >>> >> The expressions " {" " and " "} " both are included in the >pattern. >>> >> >>> >> For example, the lookup of the above pattern in the text " >>> >> {"asaf455%"}57573blabla " will result in {"asaf455%"} >>> >> >>> >> Any help will be highly appreciated. >>> >> >>> >> Thanks, >>> >> >>> >> __ >>> >> R-help@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@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. -- Sent from my phone. Please excuse my brevity. __ R-help@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.
Re: [R] A question on regular expression
A quick question. Could you please explain the -- [^}]* -- part in finding the pattern? On Fri, Sep 13, 2019 at 12:19 AM Bert Gunter wrote: > > > You can't use the same regex for str_extract_all as I used for sub (or gsub, > which is what is required here)! If you do this sort of thing a lot, you > *must* learn more about regex's. > > Anyway, this will do what you want I think: > > z <- paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " ") ## just for > readability > > > str_extract_all(z,"\\{[^}]*\\}") > [[1]] > [1] "{cd$ }" "{cad$ }" > > Cheers, > Bert > > On Thu, Sep 12, 2019 at 10:12 AM Christofer Bogaso > wrote: >> >> Thanks Bert, >> >> This works, but if in my text there are more than one patterns then >> fails to generate desired result. >> >> library(stringr) >> str_extract_all(paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " "), >> ".*(\\{.*\\}).*") >> >> This generates below - >> >> [[1]] >> >> [1] "ab{cd$ }ed ab{cad$ }ed" >> >> I was expecting I would get a vector of length 2 with desired pattern. >> >> Where did I make any mistake? >> >> Thanks, >> >> On Thu, Sep 12, 2019 at 10:29 PM Bert Gunter wrote: >> > >> > > sub(".*(\\{.*\\}).*", "\\1","ab{cd$ }ed") >> > [1] "{cd$ }" >> > >> > Use ".+" instead of ".*" within the {} if you don't want to return empty >> > {}'s. >> > >> > You might wish to use the stringr package for string matching and >> > manipulation, as it provides a more user friendly and consistent interface >> > to these tasks. >> > >> > >> > Bert Gunter >> > >> > "The trouble with having an open mind is that people keep coming along and >> > sticking things into it." >> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> > >> > >> > On Thu, Sep 12, 2019 at 9:31 AM Christofer Bogaso >> > wrote: >> >> >> >> Hi, >> >> >> >> I am wondering on what is the correct way to select a pattern which goes >> >> as - >> >> >> >> {"(any character with any length)"} >> >> >> >> The expressions " {" " and " "} " both are included in the pattern. >> >> >> >> For example, the lookup of the above pattern in the text " >> >> {"asaf455%"}57573blabla " will result in {"asaf455%"} >> >> >> >> Any help will be highly appreciated. >> >> >> >> Thanks, >> >> >> >> __ >> >> R-help@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@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.
Re: [R] A question on regular expression
Awesome, thanks! On Fri, Sep 13, 2019 at 12:19 AM Bert Gunter wrote: > > > You can't use the same regex for str_extract_all as I used for sub (or gsub, > which is what is required here)! If you do this sort of thing a lot, you > *must* learn more about regex's. > > Anyway, this will do what you want I think: > > z <- paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " ") ## just for > readability > > > str_extract_all(z,"\\{[^}]*\\}") > [[1]] > [1] "{cd$ }" "{cad$ }" > > Cheers, > Bert > > On Thu, Sep 12, 2019 at 10:12 AM Christofer Bogaso > wrote: >> >> Thanks Bert, >> >> This works, but if in my text there are more than one patterns then >> fails to generate desired result. >> >> library(stringr) >> str_extract_all(paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " "), >> ".*(\\{.*\\}).*") >> >> This generates below - >> >> [[1]] >> >> [1] "ab{cd$ }ed ab{cad$ }ed" >> >> I was expecting I would get a vector of length 2 with desired pattern. >> >> Where did I make any mistake? >> >> Thanks, >> >> On Thu, Sep 12, 2019 at 10:29 PM Bert Gunter wrote: >> > >> > > sub(".*(\\{.*\\}).*", "\\1","ab{cd$ }ed") >> > [1] "{cd$ }" >> > >> > Use ".+" instead of ".*" within the {} if you don't want to return empty >> > {}'s. >> > >> > You might wish to use the stringr package for string matching and >> > manipulation, as it provides a more user friendly and consistent interface >> > to these tasks. >> > >> > >> > Bert Gunter >> > >> > "The trouble with having an open mind is that people keep coming along and >> > sticking things into it." >> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> > >> > >> > On Thu, Sep 12, 2019 at 9:31 AM Christofer Bogaso >> > wrote: >> >> >> >> Hi, >> >> >> >> I am wondering on what is the correct way to select a pattern which goes >> >> as - >> >> >> >> {"(any character with any length)"} >> >> >> >> The expressions " {" " and " "} " both are included in the pattern. >> >> >> >> For example, the lookup of the above pattern in the text " >> >> {"asaf455%"}57573blabla " will result in {"asaf455%"} >> >> >> >> Any help will be highly appreciated. >> >> >> >> Thanks, >> >> >> >> __ >> >> R-help@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@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.
Re: [R] A question on regular expression
You can't use the same regex for str_extract_all as I used for sub (or gsub, which is what is required here)! If you do this sort of thing a lot, you *must* learn more about regex's. Anyway, this will do what you want I think: z <- paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " ") ## just for readability > str_extract_all(z,"\\{[^}]*\\}") [[1]] [1] "{cd$ }" "{cad$ }" Cheers, Bert On Thu, Sep 12, 2019 at 10:12 AM Christofer Bogaso < bogaso.christo...@gmail.com> wrote: > Thanks Bert, > > This works, but if in my text there are more than one patterns then > fails to generate desired result. > > library(stringr) > str_extract_all(paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " "), > ".*(\\{.*\\}).*") > > This generates below - > > [[1]] > > [1] "ab{cd$ }ed ab{cad$ }ed" > > I was expecting I would get a vector of length 2 with desired pattern. > > Where did I make any mistake? > > Thanks, > > On Thu, Sep 12, 2019 at 10:29 PM Bert Gunter > wrote: > > > > > sub(".*(\\{.*\\}).*", "\\1","ab{cd$ }ed") > > [1] "{cd$ }" > > > > Use ".+" instead of ".*" within the {} if you don't want to return empty > {}'s. > > > > You might wish to use the stringr package for string matching and > manipulation, as it provides a more user friendly and consistent interface > to these tasks. > > > > > > Bert Gunter > > > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > > > > On Thu, Sep 12, 2019 at 9:31 AM Christofer Bogaso < > bogaso.christo...@gmail.com> wrote: > >> > >> Hi, > >> > >> I am wondering on what is the correct way to select a pattern which > goes as - > >> > >> {"(any character with any length)"} > >> > >> The expressions " {" " and " "} " both are included in the pattern. > >> > >> For example, the lookup of the above pattern in the text " > >> {"asaf455%"}57573blabla " will result in {"asaf455%"} > >> > >> Any help will be highly appreciated. > >> > >> Thanks, > >> > >> __ > >> R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] A question on regular expression
Thanks Bert, This works, but if in my text there are more than one patterns then fails to generate desired result. library(stringr) str_extract_all(paste("ab{cd$ }ed", "ab{cad$ }ed", collapse = " "), ".*(\\{.*\\}).*") This generates below - [[1]] [1] "ab{cd$ }ed ab{cad$ }ed" I was expecting I would get a vector of length 2 with desired pattern. Where did I make any mistake? Thanks, On Thu, Sep 12, 2019 at 10:29 PM Bert Gunter wrote: > > > sub(".*(\\{.*\\}).*", "\\1","ab{cd$ }ed") > [1] "{cd$ }" > > Use ".+" instead of ".*" within the {} if you don't want to return empty {}'s. > > You might wish to use the stringr package for string matching and > manipulation, as it provides a more user friendly and consistent interface to > these tasks. > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Thu, Sep 12, 2019 at 9:31 AM Christofer Bogaso > wrote: >> >> Hi, >> >> I am wondering on what is the correct way to select a pattern which goes as - >> >> {"(any character with any length)"} >> >> The expressions " {" " and " "} " both are included in the pattern. >> >> For example, the lookup of the above pattern in the text " >> {"asaf455%"}57573blabla " will result in {"asaf455%"} >> >> Any help will be highly appreciated. >> >> Thanks, >> >> __ >> R-help@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@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.
Re: [R] A question on regular expression
> sub(".*(\\{.*\\}).*", "\\1","ab{cd$ }ed") [1] "{cd$ }" Use ".+" instead of ".*" within the {} if you don't want to return empty {}'s. You might wish to use the stringr package for string matching and manipulation, as it provides a more user friendly and consistent interface to these tasks. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Sep 12, 2019 at 9:31 AM Christofer Bogaso < bogaso.christo...@gmail.com> wrote: > Hi, > > I am wondering on what is the correct way to select a pattern which goes > as - > > {"(any character with any length)"} > > The expressions " {" " and " "} " both are included in the pattern. > > For example, the lookup of the above pattern in the text " > {"asaf455%"}57573blabla " will result in {"asaf455%"} > > Any help will be highly appreciated. > > Thanks, > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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] A question on regular expression
Hi, I am wondering on what is the correct way to select a pattern which goes as - {"(any character with any length)"} The expressions " {" " and " "} " both are included in the pattern. For example, the lookup of the above pattern in the text " {"asaf455%"}57573blabla " will result in {"asaf455%"} Any help will be highly appreciated. Thanks, __ R-help@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.