捕获两个单词之间的正则表达式

本文关键字:单词 之间 正则表达式 两个 | 更新日期: 2023-09-27 18:28:19

字符串中有以下模式

[[at the Location ]]
[[Location at]]
[[Location]]

我想取代[[at the Location ]]是从家里的例子我试过

var result = Regex.Match(equivalentSentense, @"'[[(.*?)' ]]");

但这将返回第一模式——关于如何仅替换单词位置并移除CCD_ 2或CCD_。

捕获两个单词之间的正则表达式

你可以试试这样的东西:

using System;
using System.Text.RegularExpressions;
public class Program
 {
    public static void Main()
    {
        var equivalentSentence = "[[at the Location ]] [[Location at]] [[Location]]";       
        Regex regex = new Regex(@"'['[(?<location>(.*?)) ']']");
        Match match = regex.Match(equivalentSentence);
        if (match.Success)
        {
            var location = match.Groups["location"].Value;
            Console.WriteLine(location);
        }
    }
}