【问题标题】:How to get rid of duplicates in regex如何摆脱正则表达式中的重复项
【发布时间】:2014-01-14 19:58:51
【问题描述】:

假设我有一个字符串,“cats cat cat and dogs dogs dogs”。

我会使用什么正则表达式来将该字符串替换为“cats and dogs”。即删除重复项。然而,该表达式只能删除一个接一个的重复项。例如:

“猫猫猫狗狗猫猫狗狗”

会返回:

“猫狗和猫狗”

【问题讨论】:

标签: c# regex duplicates


【解决方案1】:
resultString = Regex.Replace(subjectString, @"\b(\w+)(?:\s+\1\b)+", "$1");

将在一次调用中完成所有替换。

解释:

\b                 # assert that we are at a word boundary
                   # (we only want to match whole words)
(\w+)              # match one word, capture into backreference #1
(?:                # start of non-capturing, repeating group
   \s+             # match at least one space
   \1              # match the same word as previously captured
   \b              # as long as we match it completely
)+                 # do this at least once

【讨论】:

  • 蒂姆,你是正则表达式大师。尊重! :)
  • +1,因为表达式有效,而且解释了。
【解决方案2】:

(\w+)\s+\1 替换为$1

循环执行此操作,直到找不到更多匹配项。设置global 标志是不够的,因为它不会替换cats cats cats 中的第三个cats

正则表达式中的\1 指的是第一个捕获的组的内容。

试试:

str = "cats cats cats and dogs dogs dogs and cats cats and dogs dogs";
str = Regex.Replace(str, @"(\b\w+\b)\s+(\1(\s+|$))+", "$1 ");
Console.WriteLine(str);

【讨论】:

  • 我正在使用这个代码:replacer = Regex.Replace(replacer, @"([\\n]+)[\s+]?\1", string.Empty);但它似乎不起作用。尽管rubular.com/r/Ey6wrLYXNw 它在rubular 中工作
  • @Emmanuel 试试str = Regex.Replace(str, @"(\w+)\s+\1", "$1");
【解决方案3】:

毫无疑问,可能有一个更小的正则表达式,但这个似乎可以解决问题:

string somestring = "cats cats cats and dogs dogs dogs and cats cats and dogs dogs";
Regex regex = new Regex(@"(\w+)\s(?:\1\s)*(?:\1(\s|$))");
string result = regex.Replace(somestring, "$1$2");

它还考虑了最后不以空格结尾的“狗”。

【讨论】:

  • 这将删除太多空格:cats cats cats and dogs dogs dogs and cats cats and dogs dogs 变为 catsand dogsand catsand dogs。它也匹配太多:Michael Bolton on CD 变为 Michael BoltonCD。对办公空间参考感到抱歉。
  • 奇怪,我似乎无法重现这些错误。也许我应该添加更多的天赋:]
  • 糟糕,我错过了您正在替换为$1$2,所以我认为我看到的第一个问题不存在。但迈克尔博尔顿仍然有问题。也许一些催眠会有所帮助(或在\w 之前的单词边界\b)。
【解决方案4】:

试试下面的代码。



using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1 { /// <summary> ///
/// A description of the regular expression: ///
/// Match expression but don't capture it. [^|\s+] /// Select from 2 alternatives /// Beginning of line or string /// Whitespace, one or more repetitions /// [1]: A numbered capture group. [(\w+)(?:\s+|$)] /// (\w+)(?:\s+|$) /// [2]: A numbered capture group. [\w+] /// Alphanumeric, one or more repetitions /// Match expression but don't capture it. [\s+|$] /// Select from 2 alternatives /// Whitespace, one or more repetitions /// End of line or string /// [3]: A numbered capture group. [\1|\2], one or more repetitions /// Select from 2 alternatives /// Backreference to capture number: 1 /// Backreference to capture number: 2 ///
/// /// </summary> class Class1 { /// /// Point d'entrée principal de l'application. /// static void Main(string[] args) { Regex regex = new Regex( "(?:^|\s+)((\w+)(?:\s+|$))(\1|\2)+", RegexOptions.IgnoreCase | RegexOptions.Compiled ); string str = "cats cats cats and dogs dogs dogs and cats cats and dogs dogs"; string regexReplace = " $1";

Console.WriteLine("Before :" + str); str = regex.Replace(str,regexReplace); Console.WriteLine("After :" + str); } }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多