in reply to RegEx filter \s ! between labels, part 2

Previous code was eliminating newlines. This one is matching a literal "space" instead of "\s".

It is better documented, too :)

$text =~ s/ [ ]+ | # spaces, or: ( # begin $1 STARTPRESERVE(\d?) # STARTPRESERVE + $2 .*? # anything STOPPRESERVE\2 # STOPPRESERVE + $2 ) # end $1 /${ [$1,' '] }[ ! $1 ]/sgx; # [$1,' '] is an unnamed array. # ! $1 is: # 0 if $1 exists; # 1 if spaces were found.

Replies are listed 'Best First'.
Re: Re: RegEx filter \s ! between labels, part 2
by gryphon (Abbot) on Oct 03, 2002 at 16:36 UTC

    Er... OK, but this looks and functions like what I had before. How do I incorporate multiple PRESERVE tags? For example, I need to s/\s+/ /g everywhere except between MARKA and MARKB, between STARTPRESERVE and STOPPRESERVE, and between YETANOTHERMARKER and YETANOTHERMARKEREND.

    I can't loop through an array of regexes because they'd just cancel eachother out and do the s/\s+/ /g everywhere. I think I need a single regex, or perhaps a pulling appart of the string into an array split by the various markers, then each element analysed. What do you think is the best approach?

    ## START PSEUDO-CODE my $string = $big_string_from_my_original_example; my @array_of_stuff = split /\b(?:STARTPRESERVE)|(?:STOPPRESERVE)\b| \b(?:MARKA)|(?:MARKB)\b/, $string); foreach (@array_of_stuff) { if (&check_for_marker) { push @new_array, $_; next; } s/\s+/ /g; push @new_array, $_; } $string = join '', @new_array; ## END PSEUDO-CODE

    Oh, and I actually do need to remove line breaks but keep the markers in place. And although it's theoretically possible, I highly doubt any markers will ever be nested. (If they are, it's due to user error.)

    gryphon
    code('Perl') || die;