| 9 |
sub pchars { |
sub pchars { |
| 10 |
my($t) = ""; |
my($t) = ""; |
| 11 |
|
|
| 12 |
foreach $c (split(//, @_[0])) |
foreach $c (split(//, $_[0])) |
| 13 |
{ |
{ |
| 14 |
if (ord $c >= 32 && ord $c < 127) { $t .= $c; } |
if (ord $c >= 32 && ord $c < 127) { $t .= $c; } |
| 15 |
else { $t .= sprintf("\\x%02x", ord $c); } |
else { $t .= sprintf("\\x%02x", ord $c); } |
| 39 |
} |
} |
| 40 |
else { $outfile = "STDOUT"; } |
else { $outfile = "STDOUT"; } |
| 41 |
|
|
| 42 |
printf($outfile "Perl Regular Expressions\n\n"); |
printf($outfile "Perl $] Regular Expressions\n\n"); |
| 43 |
|
|
| 44 |
# Main loop |
# Main loop |
| 45 |
|
|
| 53 |
|
|
| 54 |
$pattern = $_; |
$pattern = $_; |
| 55 |
|
|
|
$delimiter = substr($_, 0, 1); |
|
| 56 |
while ($pattern !~ /^\s*(.).*\1/s) |
while ($pattern !~ /^\s*(.).*\1/s) |
| 57 |
{ |
{ |
| 58 |
printf " > " if $infile eq "STDIN"; |
printf " > " if $infile eq "STDIN"; |
| 64 |
chomp($pattern); |
chomp($pattern); |
| 65 |
$pattern =~ s/\s+$//; |
$pattern =~ s/\s+$//; |
| 66 |
|
|
| 67 |
|
# The private /+ modifier means "print $' afterwards". We use it |
| 68 |
|
# only on the end of patterns to make it easy to chop off here. |
| 69 |
|
|
| 70 |
|
$showrest = ($pattern =~ s/\+(?=[a-z]*$)//); |
| 71 |
|
|
| 72 |
# Check that the pattern is valid |
# Check that the pattern is valid |
| 73 |
|
|
| 74 |
eval "\$_ =~ ${pattern}"; |
eval "\$_ =~ ${pattern}"; |
| 75 |
if ($@) |
if ($@) |
| 76 |
{ |
{ |
| 77 |
printf $outfile "Error: $@\n"; |
printf $outfile "Error: $@"; |
| 78 |
next NEXT_RE; |
next NEXT_RE; |
| 79 |
} |
} |
| 80 |
|
|
| 81 |
|
# If the /g modifier is present, we want to put a loop round the matching; |
| 82 |
|
# otherwise just a single "if". |
| 83 |
|
|
| 84 |
|
$cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if"; |
| 85 |
|
|
| 86 |
|
# If the pattern is actually the null string, Perl uses the most recently |
| 87 |
|
# executed (and successfully compiled) regex is used instead. This is a |
| 88 |
|
# nasty trap for the unwary! The PCRE test suite does contain null strings |
| 89 |
|
# in places - if they are allowed through here all sorts of weird and |
| 90 |
|
# unexpected effects happen. To avoid this, we replace such patterns with |
| 91 |
|
# a non-null pattern that has the same effect. |
| 92 |
|
|
| 93 |
|
$pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/); |
| 94 |
|
|
| 95 |
# Read data lines and test them |
# Read data lines and test them |
| 96 |
|
|
| 97 |
for (;;) |
for (;;) |
| 106 |
|
|
| 107 |
last if ($_ eq ""); |
last if ($_ eq ""); |
| 108 |
|
|
| 109 |
$_ = eval "\"$_\""; # To get escapes processed |
$x = eval "\"$_\""; # To get escapes processed |
| 110 |
|
|
| 111 |
|
# Empty array for holding results, then do the matching. |
| 112 |
|
|
| 113 |
|
@subs = (); |
| 114 |
|
|
| 115 |
$ok = 0; |
eval "${cmd} (\$x =~ ${pattern}) {" . |
| 116 |
eval "if (\$_ =~ ${pattern}) {" . |
"push \@subs,\$&;" . |
| 117 |
"\$z = \$&;" . |
"push \@subs,\$1;" . |
| 118 |
"\$a = \$1;" . |
"push \@subs,\$2;" . |
| 119 |
"\$b = \$2;" . |
"push \@subs,\$3;" . |
| 120 |
"\$c = \$3;" . |
"push \@subs,\$4;" . |
| 121 |
"\$d = \$4;" . |
"push \@subs,\$5;" . |
| 122 |
"\$e = \$5;" . |
"push \@subs,\$6;" . |
| 123 |
"\$f = \$6;" . |
"push \@subs,\$7;" . |
| 124 |
"\$g = \$7;" . |
"push \@subs,\$8;" . |
| 125 |
"\$h = \$8;" . |
"push \@subs,\$9;" . |
| 126 |
"\$i = \$9;" . |
"push \@subs,\$10;" . |
| 127 |
"\$j = \$10;" . |
"push \@subs,\$11;" . |
| 128 |
"\$k = \$11;" . |
"push \@subs,\$12;" . |
| 129 |
"\$l = \$12;" . |
"push \@subs,\$13;" . |
| 130 |
"\$m = \$13;" . |
"push \@subs,\$14;" . |
| 131 |
"\$n = \$14;" . |
"push \@subs,\$15;" . |
| 132 |
"\$o = \$15;" . |
"push \@subs,\$16;" . |
| 133 |
"\$p = \$16;" . |
"push \@subs,\$'; }"; |
|
"\$ok = 1; }"; |
|
| 134 |
|
|
| 135 |
if ($@) |
if ($@) |
| 136 |
{ |
{ |
| 137 |
printf $outfile "Error: $@\n"; |
printf $outfile "Error: $@\n"; |
| 138 |
next NEXT_RE; |
next NEXT_RE; |
| 139 |
} |
} |
| 140 |
elsif (!$ok) |
elsif (scalar(@subs) == 0) |
| 141 |
{ |
{ |
| 142 |
printf $outfile "No match\n"; |
printf $outfile "No match\n"; |
| 143 |
} |
} |
| 144 |
else |
else |
| 145 |
{ |
{ |
| 146 |
@subs = ($z,$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p); |
while (scalar(@subs) != 0) |
|
$last_printed = 0; |
|
|
for ($i = 0; $i <= 17; $i++) |
|
| 147 |
{ |
{ |
| 148 |
if ($i == 0 || defined $subs[$i]) |
printf $outfile (" 0: %s\n", &pchars($subs[0])); |
| 149 |
|
printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest; |
| 150 |
|
$last_printed = 0; |
| 151 |
|
for ($i = 1; $i <= 16; $i++) |
| 152 |
{ |
{ |
| 153 |
while ($last_printed++ < $i-1) |
if (defined $subs[$i]) |
| 154 |
{ printf $outfile ("%2d: <unset>\n", $last_printed); } |
{ |
| 155 |
printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i])); |
while ($last_printed++ < $i-1) |
| 156 |
$last_printed = $i; |
{ printf $outfile ("%2d: <unset>\n", $last_printed); } |
| 157 |
|
printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i])); |
| 158 |
|
$last_printed = $i; |
| 159 |
|
} |
| 160 |
} |
} |
| 161 |
|
splice(@subs, 0, 18); |
| 162 |
} |
} |
| 163 |
} |
} |
| 164 |
} |
} |