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!
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".
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.
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).
RegEx can be effectively used to recreate patterns. So combining this with . replace means we can replace patterns and not just exact characters.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With