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

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.

Replies are listed 'Best First'.
Re^4: Splitting a string based on a regex
by Anonymous Monk on Feb 07, 2006 at 09:00 UTC
    This is almost exactly what I came up with apart from the bit about the /g flag. Thanks for your help.