用于中断字符串C#的正则表达式



这是我的字符串:

1-1 This is my first string. 1-2 This is my second string. 1-3 This is my third string.

我如何在C#中打破like;

result[0] = This is my first string.
result[1] = This is my second string.
result[2] = This is my third string.
IEnumerable<string> lines = Regex.Split(text, "(?:^|[rn]+)[0-9-]+ ").Skip(1);

编辑:如果你想在数组中得到结果,你可以做string[] result = lines.ToArray()

Regex regex = new Regex("^(?:[0-9]+-[0-9]+ )(.*?)$", RegexOptions.Multiline);
var str = "1-1 This is my first string.n1-2 This is my second string.n1-3 This is my third string.";
var matches = regex.Matches(str);
List<string> strings = matches.Cast<Match>().Select(p => p.Groups[1].Value).ToList();
foreach (var s in strings)
{
    Console.WriteLine(s);
}

我们使用多行Regex,因此^$是该行的开始和结束。我们跳过一个或多个数字、一个-、一个或几个数字和一个空间(?:[0-9]+-[0-9]+ )。我们懒惰地(*?(取所有(.(,直到行(.*?)$的末尾,懒惰地使得行$的末尾比任何字符. 都更"重要">

然后,我们使用Linq将匹配项放入CCD_ 11中。

行将以换行符、回车符或两者同时结束,这将字符串拆分为所有行结束的行。

using System.Text.RegularExpressions;
...
var lines = Regex.Split( input, "[rn]+" );

然后你可以用每一行做你想做的事。

var words = Regex.Split( line[i], "s" );

最新更新