in reply to Splitting a string based on a regex

Um, if you have a regular expression and want to split a string based on that regular expression, that's what split does. Am I missing something?

update: here's some code (untested):

# assuming you've got the RE in $re ... my @parts = split /(?=$re)/, $string;

I used a zero-width look-ahead so that the text is split just before the part that matches as that seems to be your desire.

Replies are listed 'Best First'.
Re^2: Splitting a string based on a regex
by Anonymous Monk on Feb 06, 2006 at 14:39 UTC
    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 ?

      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.
      As I suspected it's my lack of knowledge that's the problem. I figured it out.