Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>Update:</h3> <p>Your timings are a little suspect:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $text = '{{abcdefg}}' x 100_000; my @match_pos; my ($start_count, $end_count); while ( $text =~ /({{)|(}})/g ) { push @match_pos, $-[0]; if ( defined $1 ) { ++$start_count; } else { ++$end_count; } } </code></pre> <p>Let's time it:</p> <pre> C:\Temp> timethis zxc.pl TimeThis : Command Line : zxc.pl TimeThis : Elapsed Time : 00:00:00.985 </pre> <p>Replacing <code>$-[0]</code> with <code>length $`</code> takes too long to complete (I pressed <kbd>CTRL-C</kbd> after a minute).</p> <p>If I make <code>2_000</code> copies of the simple pattern above, the timings end up being the same (about .2 seconds). So, I would recommend using <code>$-[0]</code> for scalability.</p> <h3>Previous discussion</h3> <p>From <a href="http://perldoc.perl.org/perlvar.html" rel="nofollow noreferrer"><code>perldoc perlvar</code></a>:</p> <pre><code># @LAST_MATCH_START # @- </code></pre> <blockquote> <p><code>$-[0]</code> is the offset of the start of the last successful match. <code>$-[n]</code> is the offset of the start of the substring matched by n-th subpattern, or undef if the subpattern did not match.</p> </blockquote> <p>See also <code>@+</code>.</p> <p>The <code>s</code> option in your regex is unnecessary because there is no <code>.</code> in the pattern.</p> <p>Have you looked at <a href="http://search.cpan.org/perldoc/Text::Balanced" rel="nofollow noreferrer"><code>Text::Balanced</code></a>?</p> <p>You could also use <a href="http://perldoc.perl.org/functions/pos.html" rel="nofollow noreferrer"><code>pos</code></a>, although I am not sure if it will satisfy your performance requirements.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use File::Slurp; my $text = read_file \*DATA; my @match_pos; my ($start_count, $end_count); while ( $text =~ /({{)|(}})/g ) { push @match_pos, pos($text) - 2; # push @match_pos, $-[0]; # seems to be slightly faster if ( defined $1 ) { ++$start_count; } else { ++$end_count; } } for my $i ( @match_pos ) { print substr($text, $i, 2), "\n"; } __DATA__ Copy &amp; paste the source of the complicated Wikipedia page here to test. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload