perlrequick - Perl regular expressions quick start | Регулярные выражения Perl - быстрый старт. History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
| | |
perlrequick - Perl regular expressions quick start | Регулярные выражения Perl - быстрый старт. History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
| | |
This page covers the very basics of understanding, creating and using regular expressions ('regexes') in Perl. | Основы создания и использования регулярных выражений ('regexes') Perl. History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
| | |
=head2 Simple word matching | | |
The simplest regex is simply a word, or more generally, a string of characters. A regex consisting of a word matches any string that contains that word: | Самое простое регулярное выражение - это слово или, в более общем случае, символьная строка. Регулярное выражение, состоящее из слова, соответствует любой строке, которая это слово содержит: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
"Hello World" =~ /World/; # matches | "Hello World" =~ /World/; # true History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
In this statement, C<World> is a regex and the C<//> enclosing C</World/> tells perl to search a string for a match. The operator C<=~> associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match. In our case, C<World> matches the second word in C<"Hello World">, so the expression is true. This idea has several variations. | В этом примере, <World> - регулярное выражение, а <//>, обрамляющие </World/>, говорят Perl о том, что надо искать строку для соответствия этому шаблону. Оператор <=~> ассоциирует строку с шаблоном регулярного выражения и возвращает true при соответствии и false в противном случае. В нашем случае, <World> соответствует второму слову в строке <"Hello World">, поэтому выражение истинно. У этой идеи есть несколько вариаций. History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
Expressions like this are useful in conditionals: | Выражения, подобные этому, полезны в условиях: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
print "It matches\n" if "Hello World" =~ /World/; | print "It matches\n" if "Hello World" =~ /World/; History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
The sense of the match can be reversed by using C<!~> operator: | Смысл сравнения можно изменить на противоположный, используя оператор <!~>: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
print "It doesn't match\n" if "Hello World" !~ /World/; | print "It doesn't match\n" if "Hello World" !~ /World/; History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
The literal string in the regex can be replaced by a variable: | Символьную строку в регулярном выражении можно заменить переменной: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
$greeting = "World"; print "It matches\n" if "Hello World" =~ /$greeting/; | $greeting = "World"; print "It matches\n" if "Hello World" =~ /$greeting/; History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
If you're matching against C<$_>, the C<$_ =~> part can be omitted: | При сопоставлении с переменной <$_> часть <$_ =~> в выражении можно пропустить: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
$_ = "Hello World"; print "It matches\n" if /World/; | $_ = "Hello World"; print "It matches\n" if /World/; History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
Finally, the C<//> default delimiters for a match can be changed to arbitrary delimiters by putting an C<'m'> out front: | И, наконец, ограничители по умолчанию <//> можно заменить на произвольные, поставив впереди <'m'>: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
"Hello World" =~ m!World!; # matches, delimited by '!' "Hello World" =~ m{World}; # matches, note the matching '{}' "/usr/bin/perl" =~ m"/perl"; # matches after '/usr/bin', # '/' becomes an ordinary char | "Hello World" =~ m!World!; # ограничитель = ! "Hello World" =~ m{World}; # ограничители = {} "/usr/bin/perl" =~ m"/perl"; # ограничитель = ", # / здесь становится обычным символом History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
Regexes must match a part of the string I<exactly> in order for the statement to be true: | Чтобы результат был истинным, строка должна в точности содержать регулярное выражение: History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |
"Hello World" =~ /world/; # doesn't match, case sensitive "Hello World" =~ /o W/; # matches, ' ' is an ordinary char "Hello World" =~ /World /; # doesn't match, no ' ' at end | "Hello World" =~ /world/; # false, различный регистр символов "Hello World" =~ /o W/; # true, пробел (' ') - обычный символ "Hello World" =~ /World /; # false, в конце нет пробела History of edits
(Latest: magicrat 1 year, 1 month ago)
§ | |