Regular expression for rectangular brackets in R

Usually in R you can use metacharacters in a regular expression with two slashes, for example. (becomes \ (, but I believe this does not apply to square brackets.

mystring <- "abc[de" #remove [,] and $ characters gsub("[\\[\\]$]","",mystring) [1] "abc[de" 

[[:punct:]] works, but I don't like to use a non-standard regular expression if I don't need it. Can I use regular expression set syntax?

+6
source share
3 answers

You should enable perl = TRUE , then you can use a syntax similar to Perl, which is more straightforward (IMHO):

 gsub("[\\[\\]$]","",mystring, perl = TRUE) 

See demo

Result:

 [1] "abcde" 
+5
source

I would go around the syntax [ab] and use (a|b) . Besides work, it can also be more readable:

 gsub("(\\[|\\]|\\$)","",mystring) 
+4
source

You can simply use \\[ as an item for matching, you don’t need extra square brackets if you do not agree with several options:

 > mystring <- 'abc[de' > gsub("\\[", "", mystring) [1] "abcde" 

You can make it even easier and faster for single characters by removing the special meaning with fixed=TRUE :

 > mystring <- 'abc[de' > gsub("[", "", mystring, fixed=TRUE) [1] "abcde" 

Or, if the first in square brackets are square brackets (unescaped), then they are taken as an alphabetic character, and do not have the usual special meaning:

 > mystring <- 'a,bc[d]e$' > gsub("[][,$]", "", mystring) [1] "abcde" 
+1
source

Source: https://habr.com/ru/post/986378/


All Articles