bbnn
bbnn

Reputation: 3602

Removing words inside brackets with regex

I want to remove words inside bracket, I'm currently using this

【.*】

【remove this】preserve this 【remove this】

but it removes everything for this because there is another bracket

How can I solve this? it also happens on comma

◆.*、

◆ remove this、preserve this、

that regex removes everything because I have 2 commas

Upvotes: 0

Views: 990

Answers (3)

Luizgrs
Luizgrs

Reputation: 4873

You can try two solutions:

  • Lazy Operators (this might not work on your RegEx parser)

    \[.*?\]
    
    .*?,
    
  • Or replace . by a negation list to match any element but the end delimiter:

    \[[^]]*\]
    
    [^,]*,
    

Upvotes: 1

zellio
zellio

Reputation: 32484

use a specified character group

\[[^\]]+\]

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 476950

Use non-greedy matching with ?, and also escape the brackets, which are special characters:

\[.*?\]

Upvotes: 4

Related Questions