我正在尝试使用正则表达式删除重复的模式或文本。输入文本为:
RuleSet:[text],Data:[{text} {text} ...] RuleSet:[text],Data:[{text},{text},....] SomeText RuleSet:[{text } ...],数据:[{text} ...]
其中substring可以是任何字母数字,也可以包含特殊字符和空格。我试图删除以下任何内容:
RuleSet:[text],
Data:[{text}{text}...]
我想保留以下SomeText
我尝试了很多方法,但我似乎无法获得理想的结果。
答案 0 :(得分:2)
\s?(?:RuleSet|Data):\[[^]]*](?:,?\s|$)
替换为: 没有
此正则表达式将执行以下操作:
RuleSet:[text],
或Data:[{text}{text}...]
现场演示
https://regex101.com/r/eJ2aB5/1
示例文字
RuleSet:[text],Data:[{text} {text} ...] RuleSet:[text],Data:[{text},{text},....] SomeText RuleSet:[{text } ...],数据:[{text} ...]
替换后
SomeText
NODE EXPLANATION
----------------------------------------------------------------------
\s? whitespace (\n, \r, \t, \f, and " ")
(optional (matching the most amount
possible))
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
RuleSet 'RuleSet'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
Data 'Data'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
\[ '['
----------------------------------------------------------------------
[^]]* any character except: ']' (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
] ']'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
,? ',' (optional (matching the most amount
possible))
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------