Как избавиться от дубликатов в регулярных выражениях - PullRequest
5 голосов
/ 10 июня 2010

Предположим, у меня есть строка "кошки, кошки, кошки, собаки, собаки, собаки".

Какое регулярное выражение я бы использовал, чтобы заменить эту строку на "кошки и собаки".т.е. удаление дубликатов.Однако выражение должно удалять только дубликаты, которые следуют друг за другом.Например:

"кошки, кошки, кошки и собаки, собаки, собаки и кошки, кошки и собаки, собаки"

Вернется:

"кошки и собаки, кошки и собаки"

Ответы [ 4 ]

9 голосов
/ 10 июня 2010
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
2 голосов
/ 10 июня 2010

Замените (\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);
1 голос
/ 10 июня 2010

Нет сомнений, что возможно меньшее регулярное выражение, но, похоже, этот трюк делает:

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");

Также учитываются последние "собаки", не заканчивающиеся пробелом.

0 голосов
/ 10 июня 2010

Попробуйте следующий код.



using System;
using System.Text.RegularExpressions;</p>

<p>namespace ConsoleApplication1
{
   /// <summary>
   ///<br>
   ///  A description of the regular expression:
   ///<br>
   ///  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
   ///<br>
   ///
   /// </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";</p>

        Console.WriteLine("Before :" + str);

        str = regex.Replace(str,regexReplace);

        Console.WriteLine("After :" + str);
    }
}

}

...