Monday, 30 December 2013

Regular Expressions in PERL

Regualr Expressions :


           A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. Regular expressions are used when you want to search for specify lines of text containing a particular pattern.

            It is easy to search for a word or a string of characters. You can use ctrl+F in any programming editor or a word processor. Regular expressions are more powerful and flexible. You can specify insane set of conditions to match and regular expressions will do it for you like a breeze. You can search for a word with four or more vowels that end with an “s”. Numbers, punctuation characters, you name it, a regular expression can find it. Even editors support regex search, so it is beneficial to learn these and put to use.


Regex in Perl: 

Match Operator(m//) -  The m// operator is used for pattern matching. In between the forward slashes // the pattern to match is placed. Additional options, if any, are placed at the end after the last slash. If an expression is explicitly bound to the operator using the = or ! binding operators, that expression is searched for the pattern specified. If the binding operator is missing, as you will see in some later examples, = is assumed and $ is taken as the expression to be searched. In scalar context, the binding operator = returns a true value if the expression matches the pattern, an empty string (and hence a false value) if otherwise. ! simply inverts the logic so that if the expression matches the pattern a false value is returned, a true value otherwise.

Metacharacters serve specific purposes in a pattern. If any of these metacharacters are to be embedded in the pattern literally, you should quote them by pre xing it by n, similar to the idea of escaping in double-quoted string. In fact, the pattern in between the forward slashes are treated as a double-quoted string. For example, ~ m/\$/
Meta Characters: \ ,ˆ , . , $ , | , () , []

Matching Only Once -


There is also a simpler version of the match operator - the PATTERN operator. This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset.
For example, you can use this to get the first and last elements within a list:
#!/usr/bin/perl

@list = qw/food foosball subeo footnote terfoot canic footbrdige/;

foreach (@list)
{
   $first = $1 if ?(foo.*)?;
   $last = $1 if /(foo.*)/;
}
print "First: $first, Last: $last\n";

This will produce following result
First: food, Last: footbrdige

Quantifiers:
Quantifiers are used to specify how many times a certain pattern can be matched consecutively.  A quanti er can be speci ed by putting the range expression inside a pair of curly brackets. The format of which is {m[, [n]]}.

Examples:
{m} Match exactly m times

{m,} Match m or more times

{m,n} Match at least m times but not more than n times

Character Classes:
A character class includes a list of characters where matching of any of these characters result in a match of the character class. A character class is constructed by placing the characters inside a pair of square brackets.

\w        Alphanumeric characters and _ ([a-zA-Z0-9_ ])
\W        Neither alphanumeric characters nor _([ˆa-zA-Z0-9_ ])
\s         Whitespace characters ([ \t\n\r\f])
\S         Non whitespace characters ([ˆ \t\n\r\f])
\d         Numeric digits ([0-9])
\D         Non numeric digits ([ˆ0-9])

Backtracking:
Parenthesized patterns have a useful property. When pattern matching is successful, the matching substrings corresponding to the parenthesized parts are saved, which allow you to save them for further operations. For example,

$string = ’Telephone: 1234-5678’;
if ($string =˜ m/ˆTelephone:\s*(\d{4}-\d{4})$/) {
print “The telephone number extracted is ’$1’.\n”;
}

In this example, the telephone number extracted is saved as $1. There can be multiple bracketed patterns in a given pattern. The matched substrings are numbered in ascending order of position of the opening parentheses.

Substitution Operator


The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is:

s/PATTERN/REPLACEMENT/;

The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with.

For example, we can replace all occurrences of .dog. with .cat. using

$string =~ s/dog/cat/;

Translation 
Translation is similar, but not identical, to the principles of substitution, but unlike substitution, translation (or transliteration) does not use regular expressions for its search on replacement values. The translation operators are:

tr/SEARCHLIST/REPLACEMENTLIST/cds
y/SEARCHLIST/REPLACEMENTLIST/cds

The translation replaces all occurrences of the characters in SEARCHLIST with the corresponding characters in REPLACEMENTLIST. For example, using the "The cat sat on the mat." string we have been using in this chapter:

#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ tr/a/o/;
print "$string\n";
This will produce following result:
The cot sot on the mot.

Regular Expression Operators:

m//—PatternMatching - the m// operator performs pattern matching. It supports a number of options. The m option allows matching of individual lines in a multi-line string. The i option matches in a case-insensitive manner. By default, pattern matching is case sensitive. The g option attempts to carry out a global pattern matching on the string.
s///—       Search and Replace - This operator is a powerful search-and-replace engine that you can use to exibly search for certain patterns and replace it with a replacement string. The rst argument is the search pattern, just as the case of m//. The second argument is the replacement string. As you will soon see, backtracking is immensely useful in this regard.
tr///—       Global Character Transliteration - tr/// is a convenient and ef cient operator that changes a set of characters into another. The rst argument is the character list to search for. The second argument is the character replacement list. It builds a character translation map at compile time. At run-time, it changes any characters that can be found in the string into the corresponding character in the replacement list.

No comments:

Post a Comment