C# 如何使用特定的定界分割文本
我有目前的情况C# 如何使用特定的定界分割文本,c#,arrays,.net,regex,string,C#,Arrays,.net,Regex,String,我有目前的情况 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
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}" };
如何使用regex实现这一点?这里有一个使用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));
}
最后,我的代码在未限定的文本中保留任何前导或尾随空格。你可以去掉那些带有string.Trim的空格。你要在逗号上拆分的文本是动态的,它可以不带逗号,因为分隔将是{内部的任何值}挑出{文本到这里}零件很容易,只需使用组即可。选择没有分隔符的零件会更难。您可能希望选择组,然后进行第二次检查,然后选择您落后的零件。您可以使用regex.splitb.splitnew[]{{'','}',StringSplitOptions.RemoveEmptyEntries将获得没有任何花括号的零件。
private const string TheText = "before {first} middle {second} after";