
Say we wanted to check if $string contained the substring “next” or “previous” then we could use the following One way of doing this is by using the “|” or branch operation. Up until now we’ve been dealing with our strings one character at a time, but most of the time we need to be able to have more complicated options. The following example matches any lower case character: Square bracket notation also lets you specify ranges of characters, to save you having to list a whole bunch of consecutive numbers or letters, for example. If the first character inside the brackets is a “^”, rather than acting as an anchor, it negates the list, making it match anything that is not contained within the brackets, so adjusting the previous example to be true only if $string contains consonants or punctuation:

The above example is true if $string contains any vowels. A pair of square brackets will translate to any single character contained within. This will match only the words “text” and “Text”, but not for example “next”. Now what if you need to match a given set of characters, rather than any character in place of the period? Regular expressions provides a means of doing this through using square brackets. If you wanted to match only four letter strings ending in “ext” then you could combine these two like so: Similiarly the “$” character matches the end of the string: The “^” character matches the start of the string, so: We can restrict the position in which the match can occur by using anchors. This expression is not perfect, however, since it will also match parts of longer words which contain “ext”, such as “dextrous” and “flextime”. For this we use the special character “.”, a period in a regular expression tells Perl to match any single character in its place. Take the example of needing to find four letter words that end in “ext”. This is, of course, just a taste of what regular expressions can do.

This is the most basic kind of regular expression, where each character is matched literally. Will be true only if the string in the variable “$string” contains the substring “text”. The match operation returns true if the pattern is found in the string. Let’s start with the simplest regular expression operation: the match. In this primer we’ll give you a quick run down on how you can use regular expressions in your own programs to give you more power over searching and substituting text. Perl has long been an extremely popular choice for text processing due to its native regular expression support. A quick run down on how you can use regular expressions in your own programs to give you more power over searching and substituting text.
