开发者

Removing words inside brackets with regex

开发者 https://www.devze.com 2023-03-30 15:14 出处:网络
I want to remove words inside bracket, I\'m currently using this 【.*】 【remove this】preserve this 【remove this】

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


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

\[.*?\]


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:

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


use a specified character group

\[[^\]]+\]
0

精彩评论

暂无评论...
验证码 换一张
取 消