It’s all too easy to assume that everyone else knows what you know. We tend to take it for granted that all technical users are familiar with regexes. And also that, being familiar, we assume they also know how to use them to best advantage. This is not always the case, as a recent support call made clear.

The customer wanted to restrict a particular check to a particular volume, but the problem was the check script kept checking multiple other volumes with similar names. An example will make this clearer, we try to restrict the checks to running against the single vol0 colume by using the command -I vol0:

$ ./check_netapp_pro.pl Usage ‑o volume ‑H $HOST … ‑I vol0 ↵
Result code: OK
NETAPP_PRO USAGE OK ‑ 3 volumes checked
2 volumes skipped because of ‑‑include/‑‑exclude settings.
…

The script found 3 volumes to check and skipped 2. The devil is in the details. If the intention was to check all volumes with the string vol0 anywhere within their names, then this instruction is correct. However, if the intention was to restrict the checks to the single vol0 volume, as was the customer’s unfulfilled wish, then this commandline instruction is wrong.

The -I (or –include) word is not a string to compare exactly with another string, rather it is a regular expression, AKA. a regex. A regex is a description of a string, possibly using multiple meta characters, to match another string. In our example above the –include switch was looking for volumes which included the vol0 string in it’s name, and found several. If we want to pin the match down, then we have various options but the simplest is to say that the vol0 is completely contained within the start and end boundaries of the volume name we’re looking for. We do this using the special meta characters ^ (start of string) and $ (end of string). Let’s try that with our example:

$ ./check_netapp_pro.pl Usage ‑o volume ‑H $HOST … ‑I ^vol0$ ↵
Result code: OK
NETAPP_PRO USAGE OK ‑ 1 volume checked
4 volumes skipped because of ‑‑include/‑‑exclude settings.
…

That, 1 volume checked, looks much better and satisfies this particular customer’s needs exactly.

If we’d used ^vol0 we would have matched vol01 and vol02 and vol000042 as well. If we’d used vol0$ we’d have matched vol0 and vol10vol0 and myvol0 and all kinds of other variations. Being clear about what you are looking for is the first step in identifying the perfect regex.

Regular expressions are very powerful tools and it’s important to know how to use them correctly. RTFM’ing and experimentation are the best ways to learn. There are also online tools where it’s possible to see a regex in action and how it will, or will not, match a given string.

You can read a good introduction to regexes here. Our full product matrix also includes a wide range of help files, eg; Usage, which contains many useful regex tips and much more besides.