ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
6.14. Matching from Where the Last Pattern Left OffProblemYou want to match again from where the last pattern left off. This is a useful approach to take when repeatedly extracting data in chunks from a string. SolutionUse a combination of the DiscussionIf you use the while (/(\d+)/g) { print "Found $1\n"; } You can also use $n = " 49 here";
$n =~ s/\G /0/g;
print $n;
You can also make good use of while (/\G,?(\d+)/g) { print "Found number $1\n"; } By default, when your match fails (when we run out of numbers in the examples, for instance) the remembered position is reset to the start. If you don't want this to happen, perhaps because you want to continue matching from that position but with a different pattern, use the modifier $_ = "The year 1752 lost 10 days on the 3rd of September"; while (/(\d+)/gc) { print "Found number $1\n"; } if (/\G(\S+)/g) { print "Found $1 after the last number.\n"; } As you can see, successive patterns can use The location of the last successful match can be read and set with the print "The position in \$a is ", pos($a);
pos($a) = 30;
print "The position in \$_ is ", pos;
pos = 30; See AlsoThe |