Last update:   29-10-2021


For multiple lines, every line will contain \n or \n\r (windows) A variable containing text line will look something like:
$text = "This is a sample text.\n\r\n\rIt has two lines.";
We might be tempted to use ^$ for separating paraghraphs. Because those refers not to logical line positions, the next code won't work.
$text = "This is a sample text.\n\r\n\rIt has two lines.";

$text =~ s/^$/< p >/g; //won't work

print $text;
When using multiline /m modifier, the meaning of ^ and $ changes. It changes from string related to logical-line related. Also ^ and $ matches only a position, not actual charactes (like \n\r).
$text = "This is a sample text.\n\r\n\rIt has two lines.";

$text =~ s/^\s+$/< p >/mg;

print $text; 
Backslashes    (2/3)


The basic form of an email is "username\@hostname" so the regex will look like:
$text =~ s/\b(username\@hostname)\b/<a href="mailto:$1">$1<\/a>/g;
The first things to notice are the two marked backslashes (\@ and <\/a>). Use backslashes to escape forward slashes, used by Perl for search and replace. It's a little ugly, but Perl allows us to pick our own delimiters. So the escape is not necessary anymore. s|regex|replacement|modifiers OR s{regex}{replacement}modifiers
$text = "jfriedl\";

$text =~ s{(\w+\@\w+(\.\w+))}{<a href="mailto:$1">$1</a>}g;

print $text;
Readability Modifier    (3/3)

Readability modifier

Perl allows /x modifier, which allows us to rewrite regex on multiple lines. This modifier does two simple but powerful things. First, it causes most whitespaces to be ignored. You can "free-format" the expression for readability. Secondly, it allows comments with a leading #. It turns most whitespace into an "ignore me" character.
$text = "jfriedl\";

# search and replace
$text =~ s{    

    # capture the email address to $1
        # username regex
                # hosname regex
    }{< a href="mailto:$1">$1< /a>}gix; // x modifier (Wow!)

print $text; 
... 7 lines

Character classes

        A B C D E F