正则表达式、反向引用或替代结构

发布于 2024-08-12 04:20:35 字数 1126 浏览 4 评论 0原文

我正在尝试在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-08-19 04:20:35

实际上可以在 .NET 中检查匹配的括号。关键是使用平衡组。我以前听说过,所以才问这个问题。我只是不确定自己如何编写这个表达式,所以我希望一些常驻注册专家可以帮助我:)

幸运的是我找到了这个网站。其中详细解释了平衡组...他甚至提供了一个模板。所以就写到这里,供大家参考。

http://blog.stevenlevithan.com/archives/balancing-groups
该模式的要点在这里,

{
    (?>
        (?! { | } ) .
    |
        { (?<Depth>)
    |
        } (?<-Depth>)
    )*
    (?(Depth)(?!))
}

但请查看他的博客以获取详细说明。

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

{
    (?>
        (?! { | } ) .
    |
        { (?<Depth>)
    |
        } (?<-Depth>)
    )*
    (?(Depth)(?!))
}

but check out his blog for the details explanation.

北笙凉宸 2024-08-19 04:20:35

尽管我非常喜欢正则表达式,但在您的情况下它们不起作用,因为嵌套结构不是“常规”,因此无法与正则表达式匹配。您需要一个解析器来完成此类工作。对不起。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文