This subroutines count the number of capturing parentheses in a compiled regular expression. Since the regexp is compiled, we know it's correct, and thus we only have to count the opening parentheses.
Update: added /s in the while condition.
Update: This other discussion led to
a much better solution, so I commented out the original code.
#sub captures {
# local $_ = shift;
# croak "$_ is not a compiled regexp" unless ref eq 'Regexp';
# my $n = 0;
# while( /\G(?=.)/gcs ) {
# /\G[^\\(]*/gc; # ignore uninteresting stuff
# /\G(?:\\.)*/gc; # ignore backslashed stuff
# /\G\(\?/gc; # ignore special regexps
# /\G\(/gc && $n++; # a capturing (, count it!
# }
# $n;
#}
sub captures { ( @_ = '' =~ /(@{[shift]})??/ ) - 1; }
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|