[英]Get text in brackets using regular expression
我必須解析一些看起來像下面的字符串:
"some text {{text in double brackets}}{{another text}}..."
如何使用正則表達式將雙括號中的文本提取為C#中的字符串數組?
使用這個字符串
@"\{\{([^}]*)\}\}"
為您的正則表達式
var inputText = "some text {{text in double brackets}}{{another text}}...";
Regex re = new Regex(@"\{\{([^}]*)\}\}");
foreach (Match m in re.Matches(inputText))
{
Console.WriteLine(m.Value);
}
string input = @"some text {{text in double brackets}}{{another text}}...";
var matches = Regex.Matches(input, @"\{\{(.+?)\}\}")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
要從括號內獲取實際文本,請使用命名組
var r = new Regex(@"{{(?<inner>.*?)}}", RegexOptions.Multiline);
foreach(Match m in r.Matches("some text {{text in double brackets}}{{another text}}..."))
{
Console.WriteLine(m.Groups["inner"].Value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.