You are on page 1of 5

Regular Expression (Regex or Regexp or RE) (RE1.

pl)

It is a special text string for describing a search pattern within a given text
The strings are called "patterns".
Patterns are used to determine if some other string, called the "target", has (or doesn't have)
the characteristics specified by the pattern.
We call this "matching" the target string against the pattern.
Usually the match is done by having the target be the first operand, and the pattern be the
second operand, of one of the two binary operators
Pattern Binding operators (used for search)
To use the RE, Binding operators like
1. =~ (Regex Operator)
2. !~ (Negated Regex Operator) are used.
There are three regular expression operators (used for matching) within Perl:

 Match Regular Expression - m//

o In Perl, patterns can be constructed using the m// operator. In this operator, the
required pattern is simply placed between the two slashes and the binding
operators are used to search for the pattern in the specified string.
o One can use any combination of naturally matching characters to act as
delimiters for the expression. For example, m{}, m(), and m>< are all valid.

 You can omit m from m// if the delimiters are forward slashes, but for all
other delimiters you must use the m prefix.
 Note that the entire match expression, that is the expression on the left of
=~ or !~ and the match operator, returns true (in a scalar context) if the
expression matches.
$true = ($Shivnadar =~ m/Shiv/)

This evaluates to true if and only if the string in the variable $string contains

somewhere in it, the sequence of characters "a", "b", then "c".


Match Operator Modifiers

Matching Any Character with /s (RE7.pl)

By default, the dot (.) doesn’t match newline, and this makes sense for most “look

within a single line” patterns. If you might have newlines in your strings, and you
want the dot to be able to match them, the /s modifier will do the job.

Combining Option Modifiers

If you have more than one option modifier to use on the same pattern, they may be
used one after the other (their order isn’t significant):
 Substitute Regular Expression - s///
 Transliterate Regular Expression - tr///
if ($status=($bar =~/The{2,4}a/i)): here e can occur 2 or 3 or 4 times followd by a.
#if ($status=($bar =~/The*a/i)) : Here e can occur any number of times followed by a.

Regular Expression Variables (RE2.pl)


Regular expression variables include
 $&, which contains the entire matched string;
 $`, which contains everything before the matched string; and
 $', which contains everything after the matched string

The Substitution Operator (RE4.pl)


The substitution operator, s///, allows you to replace the text matched with some
new text. The basic form of the operator is :
Syntax
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.

Substitution Operator Modifiers

The Translation Operator:


The translation replaces all occurrences of the characters in SEARCHLIST with the
corresponding characters in REPLACEMENTLIST.

tr/SEARCHLIST/REPLACEMENTLIST/cds

c: #$string =~ tr/e/x/c; # except e all characters are replaced by x


d $string =~ tr/mit/x/d; It replaces mit with x if exact match is there and
delete chatacters that matches
s: It removes duplicate characters

1. Check ? along with *


2. Difference between substation and translation operator
3. Grouping
4. Word boundaries
5. Named Captures
6. Persistence of memory
7. Discussion on problem set 10 and 11
8. Update the link for both the assignments

You might also like