On Aug 13, Jeff 'japhy/Marillion' Pinyan said: >On Aug 13, ERIC Lawson - x52010 said: > >>It does change every instance (except the first) of \n%A in the scalar >>into a semicolon, given a scalar containing, e.g., > >Basically, it scrunches > >%A this >%A that >%A those > >into > >%A this;that;those The logic such a solution requires is this: "as long as we START with '%A ' and were preceded by a line that STARS with '%A ', remove the '\n%A ' and replace it with a ';'" This is difficult to do with a regex, since the "preceded by" requires a look-behind, and you can't say (?<=%A .*\n), because look-behinds cannot be a variable width (and .* is variable width). So the solution I came up with was to reverse the string. Then we are dealing with look-aheads, which CAN be variable width. $_ = reverse; s/(.*) A%\n(?=.* A%)/$1;/g; $_ = reverse; Ta da. Let me explain that. 1. reverse the string 2. the regex: a. match text (store to $1) b. match " A%\n" c. AND BE SURE we're followed by ANOTHER "... A%" string 3. replace this with the matched text and a semicolon 4. reverse the string This is a technique I have come to embrace. I call it "sexeger" (because it involves reversing regexes) and it makes things that used to be impossible with a single regex possible. The beauty of the look-ahead is that it makes sure it COULD match without actually advancing in the string. That makes this work. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: how does "1 while $scalar =~ s/(%A .*)\n%A /$1\;/g;" work?
Jeff 'japhy/Marillion' Pinyan Mon, 13 Aug 2001 09:06:18 -0700
- how does "1 while $scalar =~ s/(%A .*)\... ERIC Lawson - x52010
- Re: how does "1 while $scalar =~ s... Jeff 'japhy/Marillion' Pinyan
- Re: how does "1 while $scalar =~ s... Jeff 'japhy/Marillion' Pinyan
- Re: how does "1 while $scalar =~ s... ERIC Lawson - x52010
- Re: how does "1 while $scalar ... Jeff 'japhy/Marillion' Pinyan