How to replace brackets using regular expressions in R?

I am sure this is a very simple question. In the meantime, I am very familiar with RegEx in R, but I just can't get around this topic.

Suppose we have this line:

a <- c("ab . ) ] \"")

Now all I want to do is remove the quotation marks, the dot closing the brackets, and the closing brackets.

So I want: "ab" .

I tried:

gsub("[.\\)\"\\]]", "", a)

This does not work. It returns: "ab . ) ]" So nothing is deleted.

As soon as I exclude \\] from the search pattern, it works ...

gsub("[.\\)\"]", "", a)

But of course, he does not remove the closing brackets!

What I did wrong?!?

Thank you for your help!

+6
source share
3 answers
 a <- c('ab . ) ] "'); gsub('\\s*[].)"]\\s*','',a); ## [1] "ab" 

If you want to include the closing bracket character in the parenthesis expression, you should always include it first in parentheses; which makes it be perceived as a character inside the parenthesis expression, and not as a trailing separator for the parenthesis expression.

+2
source

Create @akruns comment

 library(stringr) str_trim(gsub('[.]|[[:punct:]]', '\\1', a)) 

replace the period in the first set of brackets with the points you want to keep.

+2
source

You can try this.

 > gsub("\\b\\W\\b(*SKIP)(*F)|\\W", "", a, perl=T) [1] "ab" > gsub("\\b(\\W)\\b|\\W", "\\1", a, perl=T) [1] "ab" 
+1
source

All Articles