[英]How can I return a list of words using regex, but define a word to also includes contents of a token?
因此,如果我的表达是:
var exp = "a b c (d e f (g h i) {foo}) {j k {l m}} n o p";
我的“令牌”分别是()和{},所以一旦您打了一个开头(或{,它应该返回所有内容,直到找到结束令牌,即使存在嵌套令牌
此示例的匹配项应为:
a
b
c
(d e f (g h i) {foo})
{j k {l m}}
n
o
p
提交的重复问题似乎并没有真正解决我匹配WORDS的问题。
var result = Regex.Matches(input, @"((\(.+\))|(\{.+\})|(\w+))")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
确实,平衡团体当然参与其中。 从使用RegEx平衡匹配括号起,我能够创建以下内容,其中我以一种在编译器中可以使用的格式进行了介绍,然后将其分解以解释发生了什么
string pattern = @"\w|\(((?<BR>\()|(?<-BR>\))|[^()]*)+\)|\{((?<BR>\{)|(?<-BR>\})|[^{}]*)+\}";
string pattern = @"
\w| #Match any word OR
\(((?<BR>\()|(?<-BR>\))|[^()]*)+\)| #Match a () balanced group OR
\{((?<BR>\{)|(?<-BR>\})|[^{}]*)+\} #Match a {} balanced group
";
这将返回我期望的8个值。 平衡组示例中缺少的是一个简单的\\ w匹配表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.