我有一条短信:
Terms: 1 I've got the {name} and {term}
So I would like to go
But not return
Terms: 2 I've got the {name} and {term}
So I would like to go
But not return
Terms: 3 I've got the {name} and {term}
So I would like to go
But not return
我想匹配每个段落,定义为以Terms:
开头,以2 or more newlines
结尾。
/(terms:).*(\n)*/gim
如何使每个段落作为一个单独的组返回?
发布于 2019-02-27 03:31:18
你可以用
(?sim)^terms:.*?(?=(?:\r?\n){2,}|\z)
详细信息
(?sim)
-启用RegexOptions.Singleline
、RegexOptions.IgnoreCase
和Regex.Multiline
选项^
-行的开始terms:
-一个文字子字符串.*?
-任何0+字符,尽可能少(?=(?:\r?\n){2,}|\z)
-一个位置,后面有两个或更多的断线序列或字符串的结尾。使用
var results = Regex.Matches(s, @"(?sim)^terms:.*?(?=(?:\r?\n){2,}|\z)")
.Cast<Match>()
.Select(x => x.Value)
.ToList();
或者,用两个或更多行分隔
(?:\r?\n){2,}
见这个.NET regex演示。它只匹配两个或更多的可选CR和LF符号的重复。
使用
var results = Regex.Split(s, @"(?:\r?\n){2,}");
发布于 2019-02-27 03:31:46
https://stackoverflow.com/questions/54904409
复制