Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace brackets using regular expressions in R?

Tags:

regex

r

I'm sure this is a really easy question. I'm quite familiar with RegEx in R in the meantime, but I just can't get my head around this one.

Suppose, we have this string:

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

Now, all I want to do is to delete the quotes, the dot, the closing paranthesis and the closing brackets.

So, I want: "a b".

I tried:

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

It doesn't work. It returns: "a b . ) ]" So nothing gets removed.

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

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

But, of course, it doesn't remove the closing brackets!

What have I done wrong?!?

Thanks for your help!

like image 334
swolf Avatar asked Jul 31 '15 11:07

swolf


People also ask

How do you use brackets in regular expressions?

For example, "/x{2,3}/" matches "xx" and "xxx". The square brackets match any one of characters inside the brackets. A range of characters in the alphabet can be matched using the hyphen. For example, "/[xyz]/ "will match any of "x", "y", or "z".

What do the [] brackets mean in regular expressions?

Square brackets ( “[ ]” ): Any expression within square brackets [ ] is a character set; if any one of the characters matches the search string, the regex will pass the test return true.

How do you escape braces in regular expression?

Find that brace and escape it with a backslash in front on it: \{ . You can also put it in a character class: [{] . You might have to do this in code for tools that you didn't write (I did it for my local autotools, for instance).

Can regex replace characters?

RegEx can be effectively used to recreate patterns. So combining this with . replace means we can replace patterns and not just exact characters.


1 Answers

a <- c('a b . ) ] "');
gsub('\\s*[].)"]\\s*','',a);
## [1] "a b"

When you want to include the close bracket character in a bracket expression you should always include it first within the brackets; that causes it to be taken as a character within the bracket expression, rather than as the closing delimiter of the bracket expression.

like image 158
bgoldst Avatar answered Oct 23 '22 05:10

bgoldst