[R] A question on regular expression
Christofer Bogaso
bog@@o@chr|@to|er @end|ng |rom gm@||@com
Thu Sep 12 19:12:45 CEST 2019
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 <bgunter.4567 using gmail.com> 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.christofer using 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 using 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.
More information about the R-help
mailing list