user3754216
user3754216

Reputation: 107

how to use gsub() to remove a complex string patterns

I had a table of data looks as following:

| _ - 9 | PR - Very happy with results. | Improvement - Be more clear regarding how the entire process works. I.e. how long you have to wait for your account to become active. | Churn Reason - none"  

I try to delete | Improvement...| from each row if there is one. I wrote the it as

feedback <- gsub("| Improvement*|", "",data$Feedback,  ignore.case = FALSE, perl = TRUE)

But it does nothing. Is anyone can help me with this?

Upvotes: 2

Views: 750

Answers (1)

merlin2011
merlin2011

Reputation: 75649

You need to escape the pipe characters |, because they are interpreted as OR. You also need a . for the * quantifier to apply to. Finally, although it does not matter for this example, you probably want the non-greedy version of * so that you don't collect more than one field worth of stuff.

Feedback = "| _ - 9 | PR - Very happy with results. | Improvement - Be more clear regarding how the entire process works. I.e. how long you have to wait for your account to become active. | Churn Reason - none"  
feedback <- gsub("\\| Improvement.*?\\|", "", Feedback,  ignore.case = FALSE, perl = TRUE)
print(feedback)

Output:

"| _ - 9 | PR - Very happy with results.  Churn Reason - none"

Upvotes: 2

Related Questions