Regular Expression Overview

A regular expression is a pattern or template for string matching. Regular expressions are written with a very specialized, powerful syntax which can perform complex string comparisons, extract desired substrings, or perform advanced search and replace operations on strings.

PBL provides support for regular expressions using syntax compatible with Perl regular expression syntax.

Regular Expression Syntax

Regular expressions are enclosed between forward slashes:
/{regular expression}/
The simplest regular expression is a single word to search for in a string. A regular expression consisting of a word matches any string that contains that word. So, the following regular expression matches any string that contains the word "hello" anywhere in the string:
/hello/

A regular expression is a pattern which is written to match single characters or multiple characters. In the case above, each one of the characters in /hello/ is simply matching itself. The next simplest matching character is the dot ".", which will match any single character except newline "\n". For example, the expression /c.t/ will match "cat", "cut", or any other three-character string starting with c and ending with t, so long as the middle character is not a newline.

If, however, you specifically wish to match only "cat" and "cut", you can use a character class, which is a pattern of characters within square brackets. The following matches "cat" and "cut", but no other word:
/c[au]t/

Many other characters and expressions are possible. They are described in various topics throughout this section.

Using Regular Expressions

In PBL, you must write regular expressions between a single quotes as follows:
'/{regular expression}/' 
In order to actually use a regular expression to do something in PBL, you must use a function which works with regular expressions. For instance, to find out if a particular string contains a word, you need to use the contains function, as follows:
myString.contains('/Hello/')

This line of code will search for the word "Hello" in the string that is contained in myString. It returns true if "Hello" is found and false if it is not.

You can also apply function calls to literal strings instead of variables. For example:
"Hello world!".contains('/Hello/') 
returns true because "Hello world!" does contain "Hello".