/usr/share/perl5/Regexp/Common/balanced.pm is in libregexp-common-perl 2011121001-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | package Regexp::Common::balanced; {
use Regexp::Common qw /pattern clean no_defaults/;
use strict;
use warnings;
use vars qw /$VERSION/;
$VERSION = '2010010201';
my %closer = ( '{'=>'}', '('=>')', '['=>']', '<'=>'>' );
my $count = -1;
my %cache;
sub nested {
my ($start, $finish) = @_;
return $Regexp::Common::balanced [$cache {$start} {$finish}]
if exists $cache {$start} {$finish};
$count ++;
my $r = '(??{$Regexp::Common::balanced ['. $count . ']})';
my @starts = map {s/\\(.)/$1/g; $_} grep {length}
$start =~ /([^|\\]+|\\.)+/gs;
my @finishes = map {s/\\(.)/$1/g; $_} grep {length}
$finish =~ /([^|\\]+|\\.)+/gs;
push @finishes => ($finishes [-1]) x (@starts - @finishes);
my @re;
local $" = "|";
foreach my $begin (@starts) {
my $end = shift @finishes;
my $qb = quotemeta $begin;
my $qe = quotemeta $end;
my $fb = quotemeta substr $begin => 0, 1;
my $fe = quotemeta substr $end => 0, 1;
my $tb = quotemeta substr $begin => 1;
my $te = quotemeta substr $end => 1;
use re 'eval';
my $add;
if ($fb eq $fe) {
push @re =>
qr /(?:$qb(?:(?>[^$fb]+)|$fb(?!$tb)(?!$te)|$r)*$qe)/;
}
else {
my @clauses = "(?>[^$fb$fe]+)";
push @clauses => "$fb(?!$tb)" if length $tb;
push @clauses => "$fe(?!$te)" if length $te;
push @clauses => $r;
push @re => qr /(?:$qb(?:@clauses)*$qe)/;
}
}
$cache {$start} {$finish} = $count;
$Regexp::Common::balanced [$count] = qr/@re/;
}
pattern name => [qw /balanced -parens=() -begin= -end=/],
create => sub {
my $flag = $_[1];
unless (defined $flag -> {-begin} && length $flag -> {-begin} &&
defined $flag -> {-end} && length $flag -> {-end}) {
my @open = grep {index ($flag->{-parens}, $_) >= 0}
('[','(','{','<');
my @close = map {$closer {$_}} @open;
$flag -> {-begin} = join "|" => @open;
$flag -> {-end} = join "|" => @close;
}
my $pat = nested @$flag {qw /-begin -end/};
return exists $flag -> {-keep} ? qr /($pat)/ : $pat;
},
version => 5.006,
;
}
1;
__END__
=pod
=head1 NAME
Regexp::Common::balanced -- provide regexes for strings with balanced
parenthesized delimiters or arbitrary delimiters.
=head1 SYNOPSIS
use Regexp::Common qw /balanced/;
while (<>) {
/$RE{balanced}{-parens=>'()'}/
and print q{balanced parentheses\n};
}
=head1 DESCRIPTION
Please consult the manual of L<Regexp::Common> for a general description
of the works of this interface.
Do not use this module directly, but load it via I<Regexp::Common>.
=head2 C<$RE{balanced}{-parens}>
Returns a pattern that matches a string that starts with the nominated
opening parenthesis or bracket, contains characters and properly nested
parenthesized subsequences, and ends in the matching parenthesis.
More than one type of parenthesis can be specified:
$RE{balanced}{-parens=>'(){}'}
in which case all specified parenthesis types must be correctly balanced within
the string.
If we are using C{-keep} (See L<Regexp::Common>):
=over 4
=item $1
captures the entire expression
=back
=head2 C<< $RE{balanced}{-begin => "begin"}{-end => "end"} >>
Returns a pattern that matches a string that is properly balanced
using the I<begin> and I<end> strings as start and end delimiters.
Multiple sets of begin and end strings can be given by separating
them by C<|>s (which can be escaped with a backslash).
qr/$RE{balanced}{-begin => "do|if|case"}{-end => "done|fi|esac"}/
will match properly balanced strings that either start with I<do> and
end with I<done>, start with I<if> and end with I<fi>, or start with
I<case> and end with I<esac>.
If I<-end> contains less cases than I<-begin>, the last case of I<-end>
is repeated. If it contains more cases than I<-begin>, the extra cases
are ignored. If either of I<-begin> or I<-end> isn't given, or is empty,
I<< -begin => '(' >> and I<< -end => ')' >> are assumed.
If we are using C{-keep} (See L<Regexp::Common>):
=over 4
=item $1
captures the entire expression
=back
=head1 SEE ALSO
L<Regexp::Common> for a general description of how to use this interface.
=head1 AUTHOR
Damian Conway (damian@conway.org)
=head1 MAINTAINANCE
This package is maintained by Abigail S<(I<regexp-common@abigail.be>)>.
=head1 BUGS AND IRRITATIONS
Bound to be plenty.
For a start, there are many common regexes missing.
Send them in to I<regexp-common@abigail.be>.
=head1 LICENSE and COPYRIGHT
This software is Copyright (c) 2001 - 2009, Damian Conway and Abigail.
This module is free software, and maybe used under any of the following
licenses:
1) The Perl Artistic License. See the file COPYRIGHT.AL.
2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2.
3) The BSD Licence. See the file COPYRIGHT.BSD.
4) The MIT Licence. See the file COPYRIGHT.MIT.
=cut
|