简体   繁体   English

使用正则表达式删除重复括号之间的内容

[英]Using Regex to delete contents between repeating brackets

I'm trying to remove unneeded words between brackets that contains certain modifier ('DeleteMe') and don't delete contents between brackets that contains other words ('DontDeleteMe').我正在尝试删除包含某些修饰符('DeleteMe')的括号之间不需要的单词,并且不要删除包含其他单词('DontDeleteMe')的括号之间的内容。 I though it was simple but proved difficult due to repeating brackets see below.我虽然很简单,但由于重复括号而被证明很困难,见下文。

[
aljdsfjfldsa      DeleteMe         aldsjflajdf
]
[
aldskjfal         DontDeleteMe     asdlkjflasdj
]
[
aljdsfjfldsa      DeleteMe         aldsjflajdf
]
[
aldskjfal         DontDeleteMe     asdlkjflasdj
]

Desired output所需 output

[
aldskjfal         DontDeleteMe     asdlkjflasdj
]
[
aldskjfal         DontDeleteMe     asdlkjflasdj
]

I tried the following but the problem is the second line will be deleted with the third line.我尝试了以下方法,但问题是第二行将与第三行一起删除。

(?s)\[.*?'DeleteMe'.*?\]

You can use a word boundary in combination with a negated character class [^您可以将单词边界与否定字符 class [^

\[[^][]*\bDontDeleteMe\b[^][]*\]

Regex demo正则表达式演示

If the word is DeleteMe you can match it using word boundaries and repace with an empty string.如果单词是DeleteMe ,您可以使用单词边界匹配它并用空字符串替换。

\[[^][]*\bDeleteMe\b[^][]*\]

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM