1
Vote

Constant string (escaped) to Function.

description

I have a function that I would like to evaluate: RegEx(test, <re>) where re is the string representation of the regular expression that I would like to evaluate. I cannot seem to find a way to pass a regular expression to the "function".
I first started out with something like
        const string skipPageExpression = "RegEx(test,'\\d{4}')";
But this gives me errors (HasErrors()).
Ideas?

comments

PhilippAeberhard wrote Jan 12, 2016 at 8:30 AM

I'm trying to do the exact same thing, but can't get it to work.

If you find any way to pass the Regex string as function parameter, let us know! :)

PhilippAeberhard wrote Jan 12, 2016 at 8:53 AM

Okay, I now found a way:

My Regex Custom function looks as follows (note how string expression is pulled from the arguments):
                if (name == "RegEx")
                {
                    var value = args.Parameters[0].Evaluate();
                    string expression = ((NCalc.Domain.ValueExpression)(args.Parameters[1].ParsedExpression)).Value.ToString();

                    Regex regex = new Regex(@expression);
                    Match match = regex.Match(value.ToString());
                    
                    args.Result = match.Success;
                }
I can now pass any Regex when all the backslashes are escaped.

Example Email RegEx('test@test.com','^([\\w\\.\\-_]+)?\\w+@[\\w-_]+(\\.\\w+){1,}$') Hope this helped you!