Perl Matching

Perl is mostly used for Regex Matching lol… Its name implies the same.

Regex Matching

if ($StringToMatch =~ /regex/)

if($StringNotToMatch !~ /regex/)

Regular Expressions

Here are some special RE characters and their meaning

.       # Any single character except a newline
^      # The beginning of the line or string
$     # The end of the line or string
*      # Zero or more of the last character
+     # One or more of the last character
?     # Zero or one of the last character

Special Characters

\n        # A newline
\t         # A tab
\w       # Any alphanumeric (word) character.
           # The same as [a-zA-Z0-9_]
\W      # Any non-word character.
           # The same as [^a-zA-Z0-9_]
\d        # Any digit. The same as [0-9]
\D        # Any non-digit. The same as [^0-9]
\s        # Any whitespace character: space,
           # tab, newline, etc
\S        # Any non-whitespace character
\b        # A word boundary, outside [] only
\B        # No word boundary


This is one important cocncept, it simplay substitues matched string with another

Eg:: To replate incorrectly formatted Data

$name =~ s/.+/\u$name/;

This will replace mahesh with Mahesh

This will replace only first occurance of match

$str =~ s/\w+/\u$&/g;

g      -     global search i.e searches every occorance and substitues

i      -      ignore case


Remember the Match

$_ = "Lord Whopper of Fibbing";

$` Contains String prior to matched String           “Lord Who”
$& Contains Matched String “pp”
$’ Containing Remaining String “er of Fibbing”


if (/(\b.+\b) \1/)
    print "Found $1 repeated\n";



During Marching the matched content in parenthesis goes into readonly variable $1,$2,…$9 so on.

$_ = “BT10978”;

Now $1 holds 10978
Same number is represented as \1 in regex

Eg: Validation of String “<b><i>this is bold italic</i></b>”

$_ ="<b><i>this is bold italic</i></b>";


        print "$1";             #prints b



About wikihead

A security freak
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s