简体   繁体   English

正则表达式以中断字符串C#

[英]Regular expression to break string C#

Here is my string: 这是我的字符串:

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

How can I break like in C# like; 我怎样才能像C#中那样破坏?

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, "(?:^|[\r\n]+)[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);
}

We use a multiline Regex, so that ^ and $ are the beginning and end of the line. 我们使用多行正则表达式,因此^$是行的开头和结尾。 We skip one or more numbers, a - , one or more numbers and a space (?:[0-9]+-[0-9]+ ) . 我们跳过一个或多个数字, - ,一个或多个数字和一个空格(?:[0-9]+-[0-9]+ ) We lazily ( *? ) take everything ( . ) else until the end of the line (.*?)$ , lazily so that the end of the line $ is more "important" than any character . 我们懒惰地( *? )取所有其他字符( . )直到行(.*?)$的末尾,从而使行$的末尾比任何字符都“重要” .

Then we put the matches in a List<string> using Linq. 然后,使用Linq将匹配项放入List<string>中。

Lines will end with newline, carriage-return or both, This splits the string into lines with all line-endings. 行将以换行符,回车符或两者都结束。这会将字符串拆分为所有行尾的行。

using System.Text.RegularExpressions;

...

var lines = Regex.Split( input, "[\r\n]+" );

Then you can do what you want with each line. 然后,您可以对每一行进行所需的操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM