在C#中使用正则表达式重复模式

问题描述 投票:1回答:1

我有一串字:

黑暗之王发光字,我们结束低翼,你好,再见

我需要找到第一个单词的最后一个字母与下一个单词的第一个字母匹配的单词(例如:worD Dark

我写了一个正则表达式:

\b\w*(\w)\W\1\w*\b

当前,它成功地连续找到2个单词(Regex.Matches[0].Value = "word dark" ; Regex.Matches[1].Value = "king glow"等)

我需要一个正则表达式,它将其作为模式(Regex.Matches[0].Value = "word dark king glow we end" ; Regex.Matches[1].Value = "low wing")读取。

我应该如何处理?

c# regex qregularexpression
1个回答
0
投票

我猜,

(?is)\w*(\w)(?= (\1)\w*)

可能会更近一些。

测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(?is)\w*(\w)(?= (\1)\w*)";
        string input = @"word dark king glow we end hello bye low wing
word Dark King Glow We End hello bye LoW wing";

        foreach (Match m in Regex.Matches(input, pattern))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.