(RE1.pl) : Regular Expression (Regex or Regexp or RE)
(RE1.pl) : Regular Expression (Regex or Regexp or RE)
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:
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
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.
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.
tr/SEARCHLIST/REPLACEMENTLIST/cds