On Tue, 20 Jan 2009 20:08:02 GMT,
wrote:
>On Thu, 15 Jan 2009 15:05:29 -0500, "Perry Aynum" <> wrote:
>
>>I am working on a SQL parser. I have a routine that recursively removes
>>enclosing parentheses and it works fine. Below is the regex that I use.
>>
>>However, I want to use the same routine, but instead of looking for
>>enclosing parens, I want to look for a string enclosed by CASE and END. Can
>>someone help me translate the regex below so that it will match a CASE/END
>>construct?
>>
>>Thanks very much.
>>
>>Parens
>>----------
>>(?:\s+)?\([^\(\)]*\)
>>
>>
>>
>>This is what I've managed so far with the CASE/END
>>
>>(?:\s+)?case(?!case|end)\s+end
>>
>
[snip explanation]
>use strict;
>use warnings;
>
>my $txt = join '', <DATA>;
>
>{
> my $cntr = 1;
>
> while ($txt =~ s/(?:\s+|^)case((?:.(?!\scase\s))*?)\s+end(\s+|$)/$1$2/is) # <- Production Regex, Ship to QA
> {
> print "\n<<<<<<<<<<< Phase".$cntr++." >>>>>>>>>>>\n";
> print "\$1= --------\n'$1'\n";
> print "\$txt= --------\n'$txt'\n";
> }
> print "\n\n************************\n FINAL:\n'$txt'\n";
>}
>
>__DATA__
>
>case
>1 case case end end
>2 case case end end
>fricases can erupt even among friends
>end
>
The regex needed a look-ahead for '\s', without it is's a bug.
///g was added to reduce passes, equals the depth of nesting now.
No more posts for a while. See ya later.
sln
-------------------------------------------------
use strict;
use warnings;
my $txt = join '', <DATA>;
my $cntr = 1;
while ($txt =~ s/(\s|^)case(?=\s)((?

?!\scase\s).)*?\s)end(\s|$)/$1$2$3/isg)
{
print "\n<<<<<<<<<<< Phase".$cntr++." >>>>>>>>>>>\n";
print "\$txt= --------\n'$txt'\n";
}
print "\n\n************************\n FINAL:\n'$txt'\n";
__DATA__
case First Line
1 case line case end spacing end
2 case case end end
3 case case END end
fricases can erupt even among friends
end
case can erupt even among end