我有当前情况
string b = "{Lorem ipsum dolor} sit amet, consectetur adipiscing elit,
Ut enim adminim veniam, quis {nostrud exercitation};
我想用这种方式分割字符串b:
string[] splittedString = new string[] {
"{Lorem ipsum dolor}",
"sit amet, consectetur adipiscing elit, Ut enim ad minim veniam, quis",
"{nostrud exercitation}" };
如何使用正则表达式实现这一目标?
答案 0 :(得分:1)
这是一个使用Regex的快捷解决方案。它还可以处理第一个{
之前和/或最后一个]
之后的文本的情况。
首先是正则表达式和要解析的字符串:
//Regex: get things that start with a {, followed by one or more non-} characters, followed by a }
private const string Pattern = @"(\{[^\}]+\})";
private const string TheText = "{Lorem ipsum dolor} sit amet, consectetur adipiscing elit, Ut enim adminim veniam, quis {nostrud exercitation}";
然后输入一些代码:
var regex = new Regex(Pattern);
var matches = regex.Matches(TheText);
var results = new List<string>();
var currentIndex = 0;
foreach (var match in matches.Cast<Match>())
{
var lastIndex = currentIndex;
//pickup any undelimited text at the beginning or between delimited groups
if (match.Index != currentIndex)
{
var unDelimited = TheText.Substring(currentIndex, match.Index - lastIndex);
results.Add(unDelimited);
currentIndex += unDelimited.Length;
}
results.Add(match.Groups[0].ToString());
currentIndex += match.Length;
}
//finally pickup any undelimited text at the end
if (TheText.Length > currentIndex)
{
results.Add(TheText.Substring(currentIndex));
}
它依靠正则表达式来找到每个{some text here}
匹配项。然后遍历那些匹配项,直接从匹配项或原始字符串的子字符串(使用匹配项的位置信息)创建一个列表。
我也用类似这样的字符串进行了测试:
private const string TheText = "before {first} middle {second} after";
最后,我的代码在未定界的文本中保留任何前导或尾随空格。您可以使用string.Trim