Thursday, February 3, 2011

vCO - use and examples of RegExp (regular expression)

Regular Expressions are a powerful tool to work with strings and - like awk - a little bit cryptic (for me). So I start to write down some use case and examples I need in vCO for searching or as an input filter, e.g. in vCO WebView.

Links I've used:
Attention: vCO does not use RegExp in format /..../, "/" must be omitted!

Example 1: IP-address format

to restrict a mandatory input only to x.x.x.x up to where x is numeric (0-9) use this RegExp (does not catch range errors, only format will be checked):

How is this build up? IP address is build by 4 groups of pattern, first three with same logic.

First three groups - 1 up to 255 always followed by a dot

A counted repetition is build by {} e.g. {3} = exact 3 times, {1,3} = min. 1 max. 3 times.
So x up to xxx is written as [0-9] <-- represents one character as a digit from 0 to 9, followed by {1,3} <-- repeat this 1 up to 3 times --> [0-9]{1,3}. The dot is a control char and mast be escaped by \. So a complete group is describes as [0-9]{1,3}\.
This group occurs exactly 3 times, so we group this expression by () and repeat it {}


Last group - 1 up to 255
same as above [0-9]{1,3}. Now combine all groups and you have a RegExp for IP-address format.


Example 2:

To search a special character combination String.index ist most used. But if you searching e.g. for VMs containing _TMPL and you want to find also _tmpl and all other combinations, you have to use "ucase" and then .index.
Or you can catch this in one RegExp (VMs is Array/VirtualMachine):

for (i in VMs)
  if (VMs[i]"_[tT][mM][pP][lL]") >= 0)
    //do something

If the pattern should only be valid at the end of string, add $ to group --> (_[tT][mM][pP][lL])$

Example 3:
match pattern for naming conventions e.g. datastore names

Another often use case is filtering datastores by name.
Assuming a company's naming convention is <CLUSTERNAME>-<TIER><LUN> where clustername has no defined length, tier is F, S, L and LUN 001 to 255. Additional Test LUNs having a suffix and should not match the search. Examples for datastore names:
  • Cluster17-L-123
  • DevCluster02-S-043
  • DevCluster01-L-155_MyTest
If you want find a valid datastore name  in tier class L or M, omitting test datastores you have to define a set of .index clauses or one RegExp:


This will find all combinations of -L-xxx and -M-xxx only if they are at the end of string.

If you have use case to be solved or other questions, feel free to leave as comment.

Regards, Andreas