正则表达式从字符串中删除重复的文本模式

时间:2016-05-28 00:38:54

标签: c# regex

我正在尝试使用正则表达式删除重复的模式或文本。输入文本为:

  

RuleSet:[text],Data:[{text} {text} ...] RuleSet:[text],Data:[{text},{text},....] SomeText RuleSet:[{text } ...],数据:[{text} ...]

其中substring可以是任何字母数字,也可以包含特殊字符和空格。我试图删除以下任何内容:

  • RuleSet:[text],
  • Data:[{text}{text}...]

我想保留以下SomeText

我尝试了很多方法,但我似乎无法获得理想的结果。

1 个答案:

答案 0 :(得分:2)

描述

\s?(?:RuleSet|Data):\[[^]]*](?:,?\s|$)

替换为: 没有

Regular expression visualization

此正则表达式将执行以下操作:

  • 找到类似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
----------------------------------------------------------------------