正则表达式解析功能和参数 [英] Regex to parse function and parameters

查看:109
本文介绍了正则表达式解析功能和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

function(param1, param2, param3, param4, ..., paramN)

我需要一个正则表达式代码来将其解析为字符串数组:

I need a Regex code to parse it as an array of strings:

function
param1
param2
param3
param4
.
.
.
paramN

我已经在网上尝试了多个示例,但似乎没有一个适合我.

I've tried several examples in the net but none of them seems to work for me.

不赞成这个建议.检查上面的代码:

Not woking with the suggestion. Check the above code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace TestRegex
{
    class Program
    {
        static void Main(string[] args)
        {
            string Expression = String.Empty;

            while (Expression.Trim() != "EXIT")
            {
                Expression = Console.ReadLine();

                ///
                /// Get function name
                /// 
                var funcTags = Regex.Matches(Expression, @"b[^()]+\((.*)\)$");

                Console.WriteLine("Matches: " + funcTags.Count);
                foreach (var item in funcTags)
                    Console.WriteLine("FuncTag: " + item);

                ///
                /// Get parameters
                /// 
                var paramTags = Regex.Matches(Expression, @"\b[^()]+\((.*)\)$");

                Console.WriteLine("Matches: " + paramTags.Count);
                foreach (var item in paramTags)
                    Console.WriteLine("ParamTag: " + item);
            }
        }
    }
}

输出:

function("a", "b", "c");
Matches: 0
Matches: 0

这是错误的...谢谢您的帮助.

Something is wrong here... Appreciate help.

推荐答案

根据函数的复杂程度,这实际上可能非常棘手.在 此处 上查看我类似的答案.要点是将正则表达式分解为两个问题.首先获取函数名称,然后获取参数.

Depending on how complex your functions can get, this can actually be quite tricky. See my similar answer over here. The gist is to break up the regex into two problems. First get the function name, then the parameters.

总而言之,这将提取函数名称:

In summary, this will extract the function name:

\b[^()]+\((.*)\)$

对于参数,它可以处理参数以及参数中的函数:

For parameters, this handles parameters as well as functions in the parameters:

([^,]+\(.+?\))|([^,]+)

这将处理嵌套函数:

(?:[^,()]+((?:\((?>[^()]+|\((?<open>)|\)(?<-open>))*(?(open)(?!))\)))*)+

但是,如果您需要支持完整的C#语法,这会变得非常棘手,例如参数中的注释等.有关讨论,请参见此处.

But, if you need to support full C# syntax, this gets tricky fast, e.g. comments in the parameters, etc. See here for a discussion.

根据评论进行更新

抱歉,我错过了一些复印错误(现已更正).您在函数正则表达式中缺少第一个\.您还使用了相同的模式来提取参数,而如上所述,您需要第二个正则表达式.另外,由于每行只有一个功能,因此您只需进行一次匹配即可.该代码将起作用:

Apologies, some errors in copying that I missed (now corrected). You were missing the first \ in the function regex. You were also using the same pattern for extracting the parameters, whereas you need the second regex as described above. Also, since there is only one function per line, you can just do the single match. This code will work:

    static void Main( string[] args )
    {
        string Expression = "function(param1, param2)";            
        ///
        /// Get function name
        /// 
        var func = Regex.Match( Expression, @"\b[^()]+\((.*)\)$" );

        Console.WriteLine( "FuncTag: " + func.Value );
        string innerArgs = func.Groups[1].Value;
        ///
        /// Get parameters
        /// 
        var paramTags = Regex.Matches( innerArgs, @"([^,]+\(.+?\))|([^,]+)" );

        Console.WriteLine( "Matches: " + paramTags.Count );
        foreach( var item in paramTags )
            Console.WriteLine( "ParamTag: " + item );

    }

输出:

FuncTag: function(param1, param2)
Matches: 2
ParamTag: param1
ParamTag:  param2

这篇关于正则表达式解析功能和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆