Regular expressions are a powerful technique for pattern matching and data extraction. The idea behind a regular expression is to describe a rule for examining a piece of text and asking, "Does this piece of text conform to a pattern?". For example, imagine a common phone number:
If we are presented with a piece of text, we may wish to ask, "Is this a valid phone number?". The regular expression language allows us to describe such a pattern which we can then match against a given piece of text. For example:
which can be simplified to:
As you can see, regular expressions can be very cryptic and would rarely be attempted to be composed by the business user, but they are in common usage by programmers and exist for every language and every platform. Given a piece of text and a pattern, we can now ask if the text matches the pattern and get a true or false answer. Further, we can extract piece of information from such a pattern match. For example, we may wish to extract just the area code.
While Automation Anywhere has some simple pattern matching capabilities, they aren't as rich as we might need for all the tasks we may need. We can augment the available commands with MetaBots that leverage DLLs. These DLLs can then leverage the .Net regular expression framework.
We have developed a MetaBot that, when called, matches text to a pattern and returns an array of results. To understand the results, imagine a piece of text that looks like:
We may want to simply know if we have a pattern that is 3 "A" some more text and 3 "C".
That pattern might be:
Using our MetaBot, we will get an array back. If the array is empty, then the pattern did not match. For example, an input of:
would return an empty array.
If the array is not empty, then the first element is the matched text.
In Regular Expressions, we can define captures. These are items in the pattern that are remembered. We create a capture by surrounding what we wish to capture in parenthesis.
would define that we wish to capture everything between "AAA" and "CCC". If we were to pass in text of:
The result would now be an array of two elements. The first would match the whole pattern while the second would contain "123" as that is what was captured. We can have multiple captures in our pattern and each capture contributes another array entry.
We have created a MetaBot that takes as input:
- pattern - The pattern to be matched.
- text - The text against which the pattern is to be matched.
which returns a one-dimensional array of matched components or null if no match occurred.
The source of the MetaBot can be found here: