minte9
LearnRemember



Delimiters

Perl allows us to pick our own delimiters.
 
#!/bin/perl -l
=begin

Backslashes are used 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
Escape is not necessary anymore

s|regex|replacement|modifiers OR 
s{regex}{replacement}modifiers

=cut

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

# <a href="mailto:jfriedl@regex.info">jfriedl@regex.info</a>

Readability

Perl allows /x modifier, to rewrite regex on multiple lines.
 
#!/bin/perl -l
=begin

Readability modifier /x

Perl allows /x modifier, to rewrite regex on multiple lines
This modifier does two simple but powerful things
First, it causes most whitespaces to be ignored
Secondly, it allows comments with a leading #
Example: Search and replace email address

=cut

$a = 'jfriedl@regex.info';
$a =~ s{    
    # capture the email address to $1
    (    
        # username regex
        [-a-zA-Z0-9._]+

        # hosname regex
        @[-a-z0-9]+(?:\.[-_a-z0-9]+)*\.(?:com|edu|info) 
    )    
    }{<a href="mailto:$1">$1<\/a>}gix; # /x

print $a; 

# <a href="mailto:jfriedl@regex.info">jfriedl@regex.info</a>



  Last update: 285 days ago