Vyacheslav Karamov пишет:
Rob Dixon пишет:
John W. Krahn wrote:
Rob Dixon wrote:
Vyacheslav Karamov wrote:
my @matches = $text =~ /$regex/g;
foreach my $arr (@matches)
{
print "$arr\n" if defined $arr;
}
You can retrieve the start and end positions of the last successful
matches from
the @- and the @+ arrays. The start and end of $1 is in $-[0] and
$+[0] for
The start and end of $1 are in $-[1] and $+[1]. $-[0] and $+[0]
contain the start and end of the last successful match.
perldoc perlvar
instance. Like this
my $regex = qr/\([^)]+\)|\[[^]]+\]/s;
while ($text =~ /($regex)/g) {
print "$-[0] - $+[0] = $1\n";
while ( $text =~ /$regex/g ) {
print "$-[0] - $+[0] = ", substr( $text, $-[0], $+[0] - $-[0]
), "\n";
Hi All!
I've solved my problem with captures, but I don't understand how to
get positions of matches:
my $regex = qr
{
(?i) # Case-insensitive
(
[\x{2022}\*]* # Any number of bullet or asterisk characters
[1-9]+ # One or more digits 1-9
\s* # Any number of spaces
(?:
\-|\,|through|and # Zero or one dash, comma or "through" or "and"
)*?
\s* # Any number of spaces
)+
}msx;
my $regex1 = qr
{
(?i) # Case-insensitive
(
(?:
figure | fig[s]?[\.]?? | table | box | chapter | diagram | scheme |
chart | plate | appendix | part | section | footnote | [p]{1,2}\.?? |
page
)
\s* # Any number of spaces
(?:
[0-9]+ # One or more digits 1-9
\s* # Any number of spaces
(?:
\-|\,|through|and|\s # Zero or one dash, comma or "through" or "and"
)*
\s* # Any number of spaces
)+
)
}msx;
my @vancouverCites =
(
"[4 5, Figure 3; 12 Chapter 4-5]",
"[8, Chapter 10]",
"[9 through 15, pp. 35-46]",
"[11, pp. 35 Through 46]",
"[see 1, 4]",
"[e.g. 2, 5]",
"[e.g. •2, ••5]",
"[e.g. *2, **5]",
"[for example 1,17]",
"[2, 9]",
);
foreach my $c (@vancouverCites)
{
$c =~ s/$regex1//g;
print "Text=\"$c\" ";
my @matches = $c =~ /$regex/g;
foreach my $arr (@matches)
{
print "Array = $arr " if defined $arr;
}
print " pos=$-[0] - $+[0]", "\n";
}
Output:
Text="[4 5, ; 12 ]" Array = 5 Array = 12 pos=12 - 11
Text="[8, ]" Array = 8 pos=5 - 2
Text="[9 through 15, ]" Array = 9 Array = 15 pos=16 - 13
Text="[11, ]" Array = 11 pos=6 - 3
Text="[see 1, 4]" Array = 1 Array = 4 pos=10 - 9
Text="[e.g. 2, 5]" Array = 2 Array = 5 pos=11 - 10
Text="[e.g. •2, ••5]" Array = •2 Array = ••5 pos=14 - 13
Text="[e.g. *2, **5]" Array = *2 Array = **5 pos=14 - 13
Text="[for example 1,17]" Array = 1 Array = 17 pos=18 - 17
Text="[2, 9]" Array = 2 Array = 9 pos=6 - 5
Why $-[0] and $+[0] are *so strange*?
Hm, I noticed strange behaviour of the loop:
foreach my $c (@vancouverCites)
{
$c =~ s/$regex1//g;
print "Text=\"$c\" ";
$c =~ /$regex/g;
print " pos=$-[0] - $+[0]", "\n";
}
in this case, output is much more close to expected one:
Text="[4 5, ; 12 ]" pos=1 - 4
Text="[8, ]" pos=1 - 2
Text="[9 through 15, ]" pos=1 - 3
Text="[11, ]" pos=1 - 3
Text="[see 1, 4]" pos=5 - 6
Text="[e.g. 2, 5]" pos=6 - 7
Text="[e.g. •2, ••5]" pos=6 - 8
Text="[e.g. *2, **5]" pos=6 - 8
Text="[for example 1,17]" pos=13 - 14
Text="[2, 9]" pos=1 - 2
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/