in reply to Re: Splitting a string based on a regex
in thread Splitting a string based on a regex

Hmm thanks for the reply but I'm not sure I understand how this will work.
The regular expression held in $exp looks like this
(?-xism:(?ig:(?:create table ([a-z]|[_])+(?{$self->{m}=1})|insert \b([ +a-z]|[_])+\b (?{$self->{m}=3})|lock table(?{$self->{m}=0})|grant(?{$s +elf->{m}=2}))))
If I understand your answer correctly (it's highly plausible that I don't !) you are suggesting that I split the line based on this regex ?
So that makes the suggested code look like this ?
my @parts = split /(?=(?-xism:(?ig:(?:create table ([a-z]|[_])+(?{$sel +f->{m}=1})|insert \b([a-z]|[_])+\b (?{$self->{m}=3})|lock table(?{$se +lf->{m}=0})|grant(?{$self->{m}=2})))))/, $text;
I'm only just past novice stage with Perl but that doesn't look right to me ?

Replies are listed 'Best First'.
Re^3: Splitting a string based on a regex
by duff (Parson) on Feb 06, 2006 at 15:37 UTC

    Well, that would work but I was thinking that you'd get the RE from Regexp::Assemble in a more direct way. Here's a complete, tested example:

    #!/usr/bin/perl use strict; use warnings; use Regexp::Assemble; open KEYS,"patterns" or die "Can't open the pattern file: $!"; my $exp = Regexp::Assemble ->new(flags => 'i',chomp => 1) ->add( <KEYS +> ); close KEYS; my $re = $exp->re; # store the assembled RE for l +ater while (my $text = <DATA>) { chomp($text); my @parts = split /(?=$re)/, $text; # split based on stored RE for my $part (@parts) { print "line $.: $part\n"; } } exit; __DATA__ insert newtab values(1) drop table newtab create table XXXX (field1 int null) insert XXXX values(1) grant select + on XXXX to sa drop table XXXX create table XXXX (field1 int null) insert XXXX values(1) grant select + on XXXX to sa drop table XXXX rollback tran create table XXXX (field1 int null) sp_help XXXX insert XXXX values(1) grant select on XXXX to sa drop table XXXX rollb +ack tran lock table cDsnJbgnd..smfJwDlwb in share mode

    I didn't write this earlier because I hadn't installed or read the docs to Regexp::Assemble yet. Also, I removed the /g flag that you passed to Regexp::Assemble->new as perl told me that it was a useless use.

      This is almost exactly what I came up with apart from the bit about the /g flag. Thanks for your help.
Re^3: Splitting a string based on a regex
by Anonymous Monk on Feb 06, 2006 at 15:33 UTC
    As I suspected it's my lack of knowledge that's the problem. I figured it out.