Nana
Nana

Reputation: 77

Gsub function in R

I have the following sentence:

trying <- 'Happy CNY to You <-U+2661> Abundance BLESSING to you and your family! <-U+2661> happy and blessed year ahead! '

I want to remove all the <.....> entities in this sentence. I used the following function:

comment = gsub(pattern = "<.*>", replacement = "", x= trying)

However, I was returned with:

"Happy CNY to You happy and blessed year ahead! "

May I know how I can edit the code such that I'll have the following:

"Happy CNY to You Abundance BLESSING to you and your family! happy and blessed year ahead! "

Upvotes: 2

Views: 786

Answers (1)

fhlgood
fhlgood

Reputation: 479

Because regex matching is greedy. You can put a ? in the expression.

comment = gsub(pattern = "<.*?>", replacement = "", x= trying)

Upvotes: 5

Related Questions