in reply to regex question

I don't get quite the same results as you with the code you pasted, but would a change like this do what you want?
- @parts = split(/($pre.*?$post)/,$text); + @parts = split(/($pre.*?%*$post)/,$text);

Replies are listed 'Best First'.
RE: Re: regex question
by joe (Acolyte) on Oct 18, 2000 at 23:15 UTC
    Thanks Fastolfe! That was the spark I needed. Here is the new code that works for me:
    $pre= "\\*\\*"; $post = "%%"; $firstchar = substr($post,0,1); $text = "**this is good%% blah **this is bad%%%%%% blah ***this is bad +%%%blah"; @parts = split(/($pre.*?$firstchar*$post)/,$text); foreach $part (@parts) { print "part: $part\n"; }
      $pre= "\\*\\*";

      You can save some eyesight by using single quotes: $pre='\*\*'

      I also came up with: my @parts = split(/($pre{2,}.*?$post{2,})/,$text); Where $pre and $post are defined as a single character.

        In addition, if $pre and $post (the two-character versions) might be changed some day, perhaps by someone that doesn't recognize/appreciate the fact that they'll be used in a regular expression and thus might not quote them, we can use the \Q and \E codes to avoid the problem altogether:
        $pre = '**'; $post = '%%'; /(\Q$pre\E.*?\Q$first\E*\Q$post\E)/