R - replace part of the string with wildcards

I was just starting to use R again, and I was wondering if there was a way to replace part of the string with wildcards.

For instance:

say that I have

S1 <- "aaaaaaaaa[aaaaa]aaaa[bbbbbbb]aaaa" 

and I want to replace everything in square brackets with "x", so a new line

 "aaaaaaaaa[x]aaaa[x]aaaa" 

Can this be done in R?

Please note that in square brackets it can be of variable length.

+6
source share
4 answers

A simple regex would look like

 \\[.+?\\] 

Example http://regex101.com/r/xE1rL1/1

Usage example

 s1 <- 'aaaaaaaaa[aaaaa]aaaa[bbbbbbb]aaaa' gsub("\\[.+?\\]", "[x]", s1) ## [1] "aaaaaaaaa[x]aaaa[x]aaaa" 

Regular expression

  • \\[ corresponds to opening [

  • .+? unwanted match

  • \\] matches closure ]

EDIT

For security, if nothing is present in [] , then the regular expression may be slightly modified as

 s1 <- 'aaaaaaaaa[]aaaa[bbbbbbb]aaaa' gsub("\\[.*?\\]", "[x]", s1) ##[1] "aaaaaaaaa[x]aaaa[x]aaaa" 
+9
source

You can also try the qdapRegex package, which has a special method for such problems: rm_square

 library(qdapRegex) S1 <- "aaaaaaaaa[aaaaa]aaaa[bbbbbbb]aaaa" rm_square(S1, replacement = "[x]") ## [1] "aaaaaaaaa[x]aaaa[x]aaaa" 

Will work the same for empty brackets

 S1 <- "aaaaaaaaa[]aaaa[bbbbbbb]aaaa" rm_square(S1, replacement = "[x]") ## [1] "aaaaaaaaa[x]aaaa[x]aaaa" 
+5
source

Use positive lookahead and lookbehind statements as shown below.

 "(?<=\\[)[^\\[\\]]*(?=\\])" 

Then replace the corresponding characters with x

 > S1<-'aaaaaaaaa[aaaaa]aaaa[bbbbbbb]aaaa' > gsub("(?<=\\[)[^\\[\\]]*(?=\\])", "x", S1, perl=TRUE) [1] "aaaaaaaaa[x]aaaa[x]aaaa" 

Explanation:

  • (?<=\\[) A positive lookbehind states that the string you want to match must precede the character [ .
  • [^\\[\\]]* Matches any character, but not [ or ] zero or more times.
  • (?=\\]) A positive lookahead states that a match must be followed by ] .
+3
source
 \\[[^\\]]+ 

You can just do it without any glances or from the very beginning. Return to [x . Read the demo.

http://regex101.com/r/yR3mM3/13

0
source

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


All Articles