Murcia has asked for the wisdom of the Perl Monks concerning the following question:

Hi Confrere, simple question! How remove in a text everything except eg. all words "book" or "cover"?
perl s/[^(book|cover)]//gms
does not work ... I want single regexp!

Thanks

Murcia

Replies are listed 'Best First'.
Re: regexp everything except
by Zaxo (Archbishop) on Aug 08, 2005 at 08:41 UTC

    You're trying to do it with a character class, which doesn't act as you want. Try matching all text up to what you capture and substituting just the capture.

    $text =~ s/.*?\b(book|cover)\b/$1/g; That will likely leave text to be removed from the end of the string. I'll leave eliminating that as an exercise.

    After Compline,
    Zaxo

Re: regexp everything except
by dtr (Scribe) on Aug 08, 2005 at 08:59 UTC

    If you're removing so much of the data from the string, why not bin it and make a new one. Something like:-

    my @out = (); while( m/(book|cover)/g ) { push(@out, $1); } my $newstr = join(" ", @out);

Re: regexp everything except
by uksza (Canon) on Aug 08, 2005 at 08:27 UTC
    Hello!
    Hmm... Is it goot enougth?
    #!/usr/bin/perl use strict; use warnings; my $zm = "ble ble book uff uff cover asdf asdfa ala book coverr"; $zm = join(' ', grep {/\b(book|cover)\b/} (split / /,$zm)); print $zm;
    greets
    uksza

    Update (\b) thanks Zaxo
Re: regexp everything except
by GrandFather (Saint) on Aug 08, 2005 at 10:33 UTC

    Check out the following Perl documentation: perlretut, perlre and perlrequick.

    Update: fix links :(

    Perl is Huffman encoded by design.
Re: regexp everything except
by Anonymous Monk on Aug 08, 2005 at 14:47 UTC
    s/[^bc]+ # Any characters that aren't a 'b' or an 'c'. |b(?!ook) # Or a 'b' which doesn't start a 'book'. |c(?!over) # Or a 'c' which doesn't start a 'cover'. /gsx;