iShare
The IntelliCorp Community Site
Using LiveCompare Regular Expressions to Validate Object Naming Conventions

Regular expressions are a powerful way to match data.  LiveCompare has several actions that support regular expressions for matching data.  The subject of this post is the Filter by Patterns action. For the sake of this discussion let's consider checking that all our custom programs conform to our naming conventions.  First let's make up some naming conventions:

Z - indicates custom program.

SD - two character 'application area'.  Possible values are: SD, MM, and FI.

_ - separator.

UK - two character 'country of use' code.  Possible values are: UK, FR and YY (which indicates global usage).

An example of a valid program name is: ZSD_UKDEMO.  An invalid program would be: ZSD_FDDEMO (the country code is invalid).

We can encode these rules in a single regular expression:

^Z(SD|MM|FI)_(UK|FR|YY)

Let's break it down.  The '^' is known as an anchor.  This one 'anchors' the expression to the beginning of the object name.  The 'Z' indicates that the first character must be a Z. Next we have a group - '(SD|MM|FI)'.  This says that the next two characters must be SD or MM or FI.  Then we have the '_' for the separator.  Lastly there's another group that checks that the next two characters in the name are UK or FR or YY.

We can make the expression a bit more efficient by sacrifcing some clarity:

^Z(?:SD|MM|FI)_(?:UK|FR|YY)

Groups - denoted by the '()' are usually capturing groups.  This means that the value of the matched characters can be referenced later in the regular expression.  Using '?:' skips this feature making the execution of the match faster.

There are many sites with tutorials and 'how-to' guides for using regular expressions.  Just put 'regular expression' into your favourite search engine.

 


Posted 09-29-2008 8:48 PM by Chris Trueman
Filed under:

Comments

Chris Trueman's Blog wrote MatchStrings Action and Capture Groups
on 01-28-2009 12:22 AM

I've posted before about using capture groups in regular expressions and how they provide a powerful