English 中文(简体)
Axapta Validation Class
原标题:

I ve written a method to handle regex validation in AX2009. Problem is that it always returns false, no matter what the expression or input string. Returns no errors, just false Mind taking a look? I m probably missing something simple.

This post has been updated to included the corrected method, without the error, so you can cut and paste the code for use in your project. BP compliant and ready for use. - Enjoy

static boolean validateMe(str regexFilter, str _testString)
{
    System.Text.RegularExpressions.Match regExMatch;
    boolean retVal;
    str regExpression;
    ;

    //See if any of the static expressions were selected
    switch (regexFilter)
    {
        case  integer  :
            regExpression =  ^\d+$ ;
            break;
        case  rgbcolor  :
            regExpression =  ^([01]?\d\d?|2[0-4]\d|25[0-5])\,([01]?\d\d?|2[0-4]\d|25[0-5])\,([01]?\d\d?|2[0-4]\d|25[0-5])$ ;
            break;
        case  number  :
            regExpression =  ^(\d+\.?\d*|\d*\.?\d+)$ ;
            break;
        case  email  :
            regExpression =  ^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$ ;
            break;
        case  phone  :
            regExpression =  ^(\()?(\d{3})(\)|-)?([0-9]{3})(-)?([0-9]{4}|[0-9]{4})$ ;
            break;
        case  nopunctationphone  :
            regExpression =  ^\d{10}$ ;
            break;
        default :
            //No static expression matched, use the passed-in value
            regExpression = regexFilter;
    }

    //see if the string matches
    if (_testString !=   )
    {
        //see if string matches expression; validation is good
        regExMatch = System.Text.RegularExpressions.Regex::Match(_testString, regExpression);
        retVal = regExMatch.get_Success();
    }
    else
    {
        //string does NOT match expression; validation fails
        retVal = false;
    }

    return retVal;
}
最佳回答

You have swapped the variables it should be:

regEx = new System.Text.RegularExpressions.Regex(regExpression);
问题回答

Could it be that you need to escape the backslashes inside strings?

regExpression =  ^\d*$ ;

etc.





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签