正则表达式、反向引用或替代结构
我正在尝试在 .NET 中编写一个正则表达式,以从看起来像这样的函数列表中捕获整个函数。
public string Test1()
{
string result = null;
foreach(var item in Entity.EntityProperties)
{
result +=string.Format("inner string with bracket{0}", "test");
}
return result;
}
public string Test5()
{
return string.Format("inner string with bracket{0}", "test");
}
public string Last()
{
return string.Format("inner string with bracket{0}", "test");
}
所以我得到了
((?<function>public string (?<fName>\w+)\(\)\s*{.*?})(?=\s*public string))
This will capture all but the last function... 或者 this
((?<function>public string (?<fName>\w+)\(\)\s*{.*?})(?=\s*(public string)|$))
This will match all functions except the 第一个。第一个函数仅部分匹配。
public string Test1()
{
string result = null;
foreach(var item in Entity.EntityProperties)
{
result +=string.Format("inner string with bracket{0}", "test");
} <-- the first capture only get to this point.
有什么想法吗?如果可能,请提供一些解释。
I am trying to write a regular expression in .NET to capture the whole function from a list of functions that look something like this.
public string Test1()
{
string result = null;
foreach(var item in Entity.EntityProperties)
{
result +=string.Format("inner string with bracket{0}", "test");
}
return result;
}
public string Test5()
{
return string.Format("inner string with bracket{0}", "test");
}
public string Last()
{
return string.Format("inner string with bracket{0}", "test");
}
So I got
((?<function>public string (?<fName>\w+)\(\)\s*{.*?})(?=\s*public string))
This will capture all but the last function... or this
((?<function>public string (?<fName>\w+)\(\)\s*{.*?})(?=\s*(public string)|$))
This will match all functions correctly except the first one. The first function is only matched partially.
public string Test1()
{
string result = null;
foreach(var item in Entity.EntityProperties)
{
result +=string.Format("inner string with bracket{0}", "test");
} <-- the first capture only get to this point.
Any idea? Please provide some explanation if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上可以在 .NET 中检查匹配的括号。关键是使用平衡组。我以前听说过,所以才问这个问题。我只是不确定自己如何编写这个表达式,所以我希望一些常驻注册专家可以帮助我:)
幸运的是我找到了这个网站。其中详细解释了平衡组...他甚至提供了一个模板。所以就写到这里,供大家参考。
http://blog.stevenlevithan.com/archives/balancing-groups
该模式的要点在这里,
但请查看他的博客以获取详细说明。
It's actually possible to do it in .NET to check for matching bracket. The key is to use a balancing group. I've heard of it before that's why I ask the question. I just wasn't sure how to write the expression myself so I was hoping that some of the resident reg expert could help me out :)
Luckily I found this website. Which explain balancing group in details... he even provide a template. So here it is for everyone else reference.
http://blog.stevenlevithan.com/archives/balancing-groups
the gist of the pattern is here
but check out his blog for the details explanation.
尽管我非常喜欢正则表达式,但在您的情况下它们不起作用,因为嵌套结构不是“常规”,因此无法与正则表达式匹配。您需要一个解析器来完成此类工作。对不起。
Although I like regexes a lot, in your case they won't work because nested structures are not "regular" and therefore can't be matched with regular expressions. You need a parser for this kind of job. Sorry.