[MUSIC PLAYING]
Today, we're talking about regular expressions in Perl:
one of the most powerful and versatile parts
of the language.
Perl has built-in support for regular expressions,
making it easy to perform complex string manipulations.
In other videos, we've seen strings in Perl
and how regular expressions work, in general.
So if you're feeling a little uncomfortable
with those topics, you may like to check out
those videos for a quick refresher.
In Perl, a regular expression is a first-class value.
Many operators and functions let us apply
regular expressions to text.
I can use '=~' to see if a value matches a regular expression.
By convention, we delimit a regular expression
with slashes.
This returns true, and this returns false.
The naked slashes here are shorthand for the match
operator, 'm'.
If I explicitly use the 'm' operator,
or any of the other quote-like regular expression operators--
I can use pairs of delimiters, like parentheses, braces,
brackets, or angle brackets.
And if I include whitespace after our 'm' operator,
I can use any character I like, so long as both the
beginning and ending character are the same.
But, in practise, you probably shouldn't actually do that.
Stick to common delimiters, like '/'s or '@'s,
or common pairs of delimiters, like braces.
A few quirks to note:
in a slash-quoted string, double quoting interpolation rules apply.
Use single quotes to disable them.
And also, watch out for the '?' delimiter, which has special meaning.
After the delimiter, we can specify flags that modify
the behaviour of the regular expression.
The full list is in 'perlre', in the Perl documentation collection,
but here's some common flags.
If I match with '/i', the expression matches
case insensitively.
If I match with '/g', globally, I match--
I find all matches in my input.
This will yield a list here of matches.
A quick side note-- if you actually want
to split your input, you probably should use
the 'split' function instead.
It takes a string, or a regular expression, to split on.
It also takes a string to split and, optionally,
how many values to produce.
Once I have that list of values,
I can use any other fun transformations I could do,
which we talk about in another video.
The opposite of 'split' is 'join', which takes a string to join
and a list of values and produces a string.
Anyway, back to the story.
Regular expressions are powerful beyond just matching
and splitting text:
I can perform substitutions.
Again, I use the '=~' operator, and the 's' operator to do
a substitution.
Many of the same flags apply here as before,
as well as a few more,
and one of the new flags that applies is '/r',
which makes a non-destructive substitution and returns
the substituted value.
To make a destructive change to our input, I simply omit the 'r'.
Perl lets us capture text, as well,
into numbered or named groups.
These are automatically assigned into '$1', '$2', '$3', and so on,
and can be used in the block of the conditional match
or in a replacement string to 's'.
To make writing simple text manipulations easier,
all of these regular expression operations that take a scalar
to the left side of that '=~', will use the magic variable '$_'
by default. 'split', 'chomp', and 'print' use it too--
amongst others--
so, for some operations, you may actually not
need any variables.
The list of functions that use '$_'
is in 'perlvar', as are many other useful special variables.
To wrap us up for now, let's put all of this together.
Here's a simple program that filters comments delimited
by brackets and dashes.
The '/s' flag here treats the new line as a normal character
for the '.' metacharacter, allowing us to match multiple lines.
So we can read the whole file into '$_' and substitute.
This expression is pretty gross.
I've written regular expressions like this
before and, usually within hours,
had no idea what they did.
So instead, let's introduce one last flag--
'/x', which makes whitespace irrelevant to the expression,
and lets us insert comments to explain ourselves.
Here's what our code looks like with '/x'.
Lovely.
For a more comprehensive view of these topics,
check out the regular expressions coverage
in the Perl documentation collection.
[MUSIC PLAYING]