4

I am trying to replace opening and closing brackets in a string. R seems to do it for the opening bracket:

> gsub("[\\[]","==","hello [world]")
[1] "hello ==world]"

but not for the closing brackets

> gsub("[\\]]","==","hello [world]")
[1] "hello [world]"

Why is this so?

highBandWidth
  • 16,751
  • 20
  • 84
  • 131
  • As far as I understood, the question is about using a `[` and `]` inside a bracket expression/character class, right? There are other answers dwelling on something else, thus, it could be a good idea to adjust the question title and question itself. – Wiktor Stribiżew Oct 05 '16 at 22:53

3 Answers3

2

Look, the pattern in gsub("[\\]]","==","hello\\] [world]"), [\]], is effectively matching a \ followed with ]. Try gsub("[\\]]","==","hello\\] [world]"), and the result will be hello== [world], the literal backslash will get replaced.

In a TRE regex pattern, a \ inside a bracket expression matches a literal backslash.

As a fix for your "[\\]]" regex, you may remove the \ from the pattern:

gsub("[[]","==","hello [world]")

See this R online demo.

You may escape it in a PCRE pattern though, as PCRE character classes allow escaped characters inside them:

gsub("[\\[]","==","hello [world]", perl=TRUE)

See another demo

If you need to replace either [ or ], just put the ][ inside the bracket expression:

 gsub("[][]","==","hello [world]")
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks. To do both, `gsub("[\\[]]","==","Hello [World]"` doesn't work but `gsub("[]\\[]","==","Hello [World]")` does. Is there a rhyme or reason for this? – highBandWidth Oct 05 '16 at 07:50
  • FYI: The `]` inside a `[...]` is treated as a literal `]` when it is placed after the open `[`, thus, the `[][]` effectively matches a literal `[` or a literal `]`. – Wiktor Stribiżew Oct 05 '16 at 07:50
  • In a TRE regex, a backslash inside a bracket expression is treated as a literal backslash. In your `"[]\\[]"`, you match either `]`, or ``\`` or `[`. See [this R demo](https://ideone.com/8C8mV0) where `gsub("[]\\[]","==", "Hello \\ [World]")` replaces 3 chars in the input string. – Wiktor Stribiżew Oct 05 '16 at 07:53
1

This simply works:

gsub("]", "==","hello [world]")
#"hello [world=="
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
1

A maybe more readable/straight forward way to do it is with stringi,

library(stringi)
stri_replace_all_regex('hello [world]', '\\[|]', '==')
#[1] "hello ==world=="
Sotos
  • 51,121
  • 6
  • 32
  • 66