2015-12-28 46 views
-2

之间捕捉我有字符串中使用以下模式正则表达式两个字

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

我想在家里我试图

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

更换[[at the Location ]]从例子,但这将返回第一模式任何想法如何只取代字位置,并删除]][[

+3

你可以提供一个样本的数据和预期的输出? –

+0

我想知道['@“\ [[(。*?)\]]”'](http://regexstorm.net/tester?p=%5c%5b%5b(。*%3f)+%5d %5d&i =%5b%5bat + +位置+%5d%5d%0d%0a%5b%5b位置+ at%5d%5d%0d%0a%5b%5bLocation%5d%5d)可以为您返回任何内容。 –

回答

1

你可以尝试这样的事:

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