Regular expressions in AX

In computing, a regular expression is a string that is used to describe or match a set of strings, according to certain syntax rules.
 
Regular expressions are used by many text editors and utilities to search and manipulate bodies of text based on certain patterns. Many programming languages support regular expressions for string manipulation. For example, Perl and Tcl have a powerful regular expression engine built directly into their syntax. The set of utilities (including the editor ed and the filter grep) provided by Unix distributions were the first to popularize the concept of regular expressions. "Regular expression" is often shortened in speech to regex, and in writing to regexp or regex (singular) or regexps, regexes, or regexen (plural).
 
As an example of the syntax, the regular expression \bex can be used to describe (and search for) all of the instances of the string "ex" that occur at word breaks (signified by the \b). Thus in the phrase, "Texts for expert experimenters," the regular expresssion \bex returns the "ex" in "expert" and "experimenters," but not in "Texts" (because the "ex" occurs inside the word there and not at the word break).
 
Many modern computing systems provide wildcard characters in matching filenames from a file system. This is a core capability of many command-line shells and is known as globbing. Wildcards differ from regular expressions in that they can only express very restrictive forms of alternation.
 
[Source for this text: www.wikipedia.org]
 
In AX too we can make use of regular expressions. Here is a sample code which can be used to validate an email address. Just copy this method to the "Global" class in your application. Please note this code will work only on AX 4.0 and above

static boolean validateEmailAddress(str 100 _str)
{
    boolean retval;
    InteropPermission permission;
    System.Text.RegularExpressions.Regex re;
    ;
 
    permission = new InteropPermission(InteropKind::ClrInterop);
    permission.assert();
 
    // BP deviation documented
    re = new System.Text.RegularExpressions.Regex("^([a-zA-Z0-9_’+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$");
 
    // BP deviation documented
    retval = CLRInterop::getAnyTypeForObject(re.IsMatch(_str));
 
    return retval;
}
 

Here is a sample job which makes use of above written method.
 

static void checkEmailAddress(Args _args)
{
    str emailaddress = "sumit.l@sumit.l.com";
    ;
    print validateEmailAddress(emailaddress);
    pause;
}

One thought on “Regular expressions in AX

  1. Hi , this is balakrishna. I got a task where I have to scan entire AOT methods and to find the path where “index hint” keywords were used in the select stmts . i am hardcoding . but , this should not be happen. if I give so, during scanning its considering the comments where index hint is used.

    or else Is there any code for eliminating commented stmts in the code during scanning.hereby I am sending the code which I wrote . help me in this regard.
    this is one task.

    the second one is use of code assert when WINAPI or WINAPI SERVER is used,
    means when ever a class is extended from codeaccesspermission ,I have to check the classes and to find the path wherever the .assert() is used in the code.
    hereby i am sending both the code .please help me in this regard.

    CODE ASSERT :
    static void job8(Args _args)
    {
    #Properties
    #define.CodeAccessPermission(“CodeAccessPermission”)
    //#define.propertyExtends(“extends”)
    SysScannerClass scan;
    TreeNodePath path;
    TreeNodeTraverser nodeTraverser;
    TreeNode treeNode;
    TreeNode childNode;
    str Name;
    treeNode = TreeNode::rootNode();
    treeNode = TreeNode::findNode(‘Classes’);
    treeNode = treeNode.AOTfirstChild();

    while(treeNode)
    {
    if(treeNode.AOTgetProperty(#PropertyExtends) == #CodeAccessPermission)
    {
    nodeTraverser = new TreeNodeTraverser(treeNode);
    while (nodeTraverser.next())
    {
    childNode = nodeTraverser.currentNode();
    if(childNode == null) break;
    scan = new SysScannerClass(childnode);
    if (strContains(scan.source(),’.assert()’))
    {
    info(childNode.treeNodePath());
    }
    }
    }
    else
    {
    treeNode = treeNode.AOTnextSibling();
    if(treeNode.AOTgetProperty(#PropertyExtends) == #CodeAccessPermission)
    {
    nodeTraverser = new TreeNodeTraverser(treeNode);
    while (nodeTraverser.next())
    {
    childNode = nodeTraverser.currentNode();
    if(childNode == null) break;
    scan = new SysScannerClass(childnode);
    if (strContains(scan.source(),’.assert()’) )
    {
    info(childNode.treeNodePath());
    }
    }
    }
    }
    treeNode=treeNode.AOTnextSibling();
    }
    }

    INDEX HINT:
    static void indexHint_1(Args _args)
    { #TokenTypes
    SysScannerClass scanner;
    TreeNode treeNode = new TreeNode();
    TreeNode childNode = new TreeNode();
    TreeNode node = new TreeNode();
    TreeNode childMethodNode;
    TreeNodeTraverser treeNodeTraverser ;
    TreeNodeName name;
    TreeNodeName childNodeName;

    int counter = 0,i;
    treeNode = TreeNode::rootNode();
    treeNode= treeNode.AOTfirstChild();
    while(treeNode)
    {
    treeNodeTraverser = new TreeNodeTraverser(treeNode);
    while (treeNodeTraverser.next())
    {
    if(treeNode == null) break;
    node = treeNodeTraverser.currentNode();
    childMethodNode = node;
    childNodeName = node.AOTname();

    if(childNodeName == ‘Methods’)
    {
    childMethodNode = childMethodNode.AOTfirstChild();
    while(childMethodNode)
    {
    if(childMethodNode == null) break;
    scanner = new SysScannerClass(childMethodNode) ;
    if(strcontains(scanner.source(), ‘index hint’))
    {
    info(childMethodNode.treeNodePath());
    counter+= 1;
    }
    childMethodNode=childMethodNode.AOTnextSibling();
    }
    }
    }
    treeNode=treeNode.AOTnextSibling();
    }
    }

    Like

Leave a comment