ru en uk

  sing in

(044) 362 48 16   (098) 294 41 60


   Services
   Portfolio
   Prices
   Articles
   Services
   Printing
   Multimedia
   Hosting
   Contacts

Home   |   WEB development   |   Articles   |   Other topics

Regular expressions in Perl

Regular expressions are used for finding patterns in strings. For example, in order to find the phone book for a particular name or, for example, all names beginningayuschiesya with the letter 'a'. Working with regular expressions is one of the most powerful and useful, and at the same time most difficult to understand the features of Perl. We hope that after reading this article you will understand how a powerful and convenient tool. Get some experience, you can useup these opportunities with great benefits for yourself.
Operators

To work with regular expressions in Perl using the three operator
- Comparison operators (matching - m / /), the operator of permutations
(substitution s / / /) and the translation operator (translation - tr / //).

All three operators are using the variable $ _ by default, so
continue until you will be presented to the operation = ~ and! ~ will
use it.

Camera comparison checks whether verifiable expression of
template, andreturns 1 if it does, and the value 0 in
otherwise. The recording of this operator consists of the letters m,
separator (usually Kosaya line - /, but in principle it can
be almost any character), a template and another separator (such as,
as the first:).

Comparison operators
$ _ =;
if (m / hello /) (
print "hello user n";
)

if ($ input ( 'siteurl') = ~ # http:// #) (
print $ input ( 'siteurl');
)

In this example, check whether the line obtained from
STANDARDartnogo entrance, the word 'hello'. If so (the operator m / / returns
value 1), the standard output will return the phrase 'hello user'.

Note: Actually, the symbol 'm' is optional, so
Operator of this example may be lookinget just how / hello /.

The operator of substitution is in line all substrings satisfying
template, and replaces them some other meaning. Record this
operator consists of the letter s, indicating that it is actually
operator of permutations and initialth (which replaced) and substitution (at
that substitute) patterns, separated by delimiters.

The operator substitutions
$ _ = 'My name is Fred';
# Oh no, my name is Jonathan!
s / Fred / Jonathan /;

In thisexample, in the line $ _ all the words of Fred will be changed to Jonathan.

The operator of translation also makes the substitution, but several other
character - he used to replace some of the individual symbol
other (defined) symbols. The syntax of this operatorRA is similar to
syntax of substitutions, with the difference that the first he
obviously starts with the letter tr, but not inserted between the separated
templates, and the group of characters, the first - the original characters, the second --
wildcard, with the appropriate symbols should stand for
aboutdinakovyh positions in their groups - if you want to replace,
such as the Latin 'm' in Cyrillic 'm', they must stand on
the same field: 'm' - in the first group of characters, 'm' - in the second.

Operator translation
$ _ = 'Hi.there, My.name.is.jonathan, ';
tr /., /! /;

In this example, all the commas will be changed to exclamation marks, and
point - the gaps.

Modifiers

The capacity of each of theseX operators can be expanded using
modifiers. Modifiers - this is roughly equivalent to the characters
appended to the operator (eg, the so - s / fred / Jonathan / i), referring to the
how he had to deal with the working value.

Modifiers for operationalratora comparison:
g - are all found substrings;
i - ignored Iskalni characters per line;
m - is considering a multi-line value;
s - is considering a line as odnostrochoe value;
x - allows you to use extended regular expressions;

Modifiers to the operator of permutations:
e - calculates the wildcard expression to substitution;
g - are all found substrings;
i - ignored Iskalni characters per line;
m - is considering a multi-line value;
s - is considering a line of SGLostrochoe value;
x - allows you to use extended regular expressions.

Modifiers

$ _ = 'My name is Fred';
s / fred / Jonathan / i; # My name is Jonathan
s / jonathan / routine () / ie; # My name is [something]
Operation = ~ and! ~

Operation = ~ and! ~ can be used with the operators m / /, s / / / and
tr / / / all variables, not just the $ _, which is used by those
operators by default.

The operator = ~ </ strong> performs the same function as the assignment operator '='
(in the case of operators with the s / / / and tr / / /) and the operator
comparison of 'eq' (when used with the operator of m / /).

Operation = ~
$ name = 'my name is Fred';

$ string = 'hello world';
if ($ string = ~ / hello / i) (
print 'helloworlded in this string.';
)

Similarly, the operation! ~ Is used as well as the operation is' ne '(her
like writing operatsii chiselnogo comparison! =), used
only with the comparison operator and a denial to meet
pattern.

Operation! ~
$ string = 'good';
if ($ string! ~ / bad /) (
print "hey, it's not too bad yet!";
)/ p>

Memory

And finally - on the possibility of more convenient to work with the results
processing of regular expressions, namely, the possession of them in separate
variables. These variables are predefined$ &, $ `, $ ', And
set of variables $ 1, $ 2, ..., $ 9.

The $ &

This variable is designed to store a fragment of a line, which
granted a pattern defined by regular expressions. It is convenient to
such casesinstances, for example, if you want to find a number in line, but
unknown what this number. Here is how it might look like:

$ string = "error 404."
$ string = ~ m / d + /;
$ number = $ &; # $ number contains a "404"

The variables $ `and $ '
These variables are used to store fragments, which do not
satisfy the pattern, ie substrings that lie before and after
a result respectively. In other words, after the operation, for example,
comparison, the value of outcomes divided into rowsthree parts - part
which came under the template, and fragments that come before it and
after it. These parts and placed in the variables $ &, $ `and $ '
respectively. (Please note that in the first variable --
reverse-quote, and the second - direct). Let's look at the previous
sample.

$ string = "error 404."
$ string = ~ m / d + /;

$ number = $ &; # $ number contains a "404"
$ before = $ `; # $ before a" error "
$ after = $ '; # $ after a "."
The variables $ 1 .. $ 9

These variables are used for storagefragments of lines that
satisfy certain fragments corresponding template. In
Pattern pieces are provided with the help of brackets. Each fragment
allocated a room in the order in which they are located, and
the corresponding variable will contain the value.
$ string = "this is to be uppercased";
$ string = ~ s / (upper w +) / uc ($ 1);
# $ String = "this is to be UPPERCASED"

$ string = "15 apples, 2 foos, 3 bars";
while ($ string = ~ m / ( d +) ( w +) / g) (
print "$ 2: $ 1 n";
)
# Outputs apples: 15
# Foos: 2
# Bars: 3


 
Paginal output
29.05.2007
Safety Fundamentals
29.05.2007
Introduction to PHP5
29.05.2007