Lesson 22h: Regular Expression: Testing your RE

Testings Your Regular Expressions

To be sure that you are getting what you think you want you can use the following
"Magic" Perl Automatic Match Variables $&, $`, and $'

Everything before the pattern begins is stored in:

$&

Everything within the pattern is stored in:

$`

Everything after the pattern ends is stored in:

$'

Code:

1
2
3
4
if ("Hello there, neighbor" =~ /\s(\w+),/){
        print "That actually matched '$&'.\n";
        print "That was ($`) ($&) ($').\n";
}

Output:

That actually matched ' there,'.
That was (Hello) ( there,) ( neighbor).

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *