Documentation for "expecto"

Download the whole documentation as one plain text file


4. Template Syntax

4.2. String Flags (b, e) and Operators

Lets look at a more complicated example. Assume we have a cron job that can produce any of the following variants of output. All of these are "normal", so our template has to match all of them:

Starting foo checks on 2012-10-27 03:15:06 File system allocation: 56% (ok) Waiting for background job to finish ... Successfully done.    
Starting foo checks on 2012-10-28 03:15:03 File system allocation: 55% (ok) Waiting for background job to finish ... Waiting for background job to finish ... Waiting for background job to finish ... Successfully done.
Starting foo checks on 2012-10-29 03:15:08 File system allocation: 61% (ok) Waiting for background job to finish ... Waiting for background job to finish ... Successfully done.  
Starting foo checks on 2012-10-30 03:15:10 File system allocation: 58% (ok) Waiting for background job to finish ... Successfully done.    

The following template will match all of these variants:

* "" "Starting foo checks on"{b} "File system allocation:"{b} && "(ok)"{e} + "Waiting for background job to finish ..." "Successfully done." * ""

Let's look at that template in detail. Again, the first and the last line skip an arbitrary number of empty lines at the beginning and at the end, respectively.

The problem with the "Starting foo checks" line is that it contains a time stamp. This time stamp is always different, so we can't match it with a fixed string. Instead, we only match the beginning of the line and ignore the time stamp. This is done with the "b" flag that is placed in curly braces and appended to the string, after the closing quote character. In other words, this flag means: This string does not match the entire line, but only the beginning. Any characters after this string are ignored.

The next line shows another feature. This time, the percent value can be different. Again, we use the "b" flag to match the beginning of the line. However, this also ignores the "(ok)" part at the end. But we would like to include that part, because it might be important: We want to know when it is not "ok". Therefore we add another check for this line, this time matching at the end. You probably guessed it already: The "e" flag matches the end of the line (the opposite of the "b" flag).

As you can see, multiple matches for the same line can be combined with logical operators. In this case, we are using the operator "&&" (read: "and") which means that both matches must be true. The expecto utility supports all logical operators known from the C language, with the same precendence and order of evaluation: "&&" ("and"), "||" ("or"), "!" ("not"), "?:" (conditional ternary operator) and "( ... )" (parentheses to override the precedence). Additionally, a logical exclusive-or operator is supported, it's spelled "^^" and its precedence is between "and" and "or". Finally, the boolean constants "true" and "false" can be used (case-insensitive). Please note the difference:

"true" true

The first template line contains a quoted string that matches an input line consisting of the word "true". The second template line contains the constant true that matches any input line, no matter what it contains.

Notes for advanced users:



[Valid XHTML 1.0]