Regular expression question : how to not match a word

Discussion in 'Perl Misc' started by linq936@hotmail.com, Jan 26, 2006.

  1. Guest

    Hi,
    I want to use regular expression to match a pattern which does not
    have a whole word, let us say the word is "trash", then I tried this
    pattern, [^trash], but it does not work. It sort of makes sense that
    this does not work in that it does not treat "trash" as a whole word.

    Any idea how to make it?

    Thanks.
    , Jan 26, 2006
    #1
    1. Advertising

  2. J. Gleixner Guest

    wrote:
    > Hi,
    > I want to use regular expression to match a pattern which does not
    > have a whole word, let us say the word is "trash", then I tried this
    > pattern, [^trash], but it does not work. It sort of makes sense that
    > this does not work in that it does not treat "trash" as a whole word.


    Give the documentation a try. This is right out of perlretut:

    "... The sense of the match can be reversed by using !~ operator:

    if ("Hello World" !~ /World/) {
    print "It doesn't match\n";
    }
    else {
    print "It matches\n";
    }

    "
    J. Gleixner, Jan 26, 2006
    #2
    1. Advertising

  3. Paul Lalli Guest

    wrote:

    > I want to use regular expression to match a pattern which does not
    > have a whole word, let us say the word is "trash", then I tried this
    > pattern, [^trash], but it does not work.


    Can you explain what made you think that might work?

    That chunk will match any ONE character that is NOT a t, an r, an s, or
    an h.

    > Any idea how to make it?


    The same way that's been recommended in this group aproximately 20
    times in the past year. Please do a minimal amount of searching before
    posting. Search for terms like "negative match", for example.

    Paul Lalli
    Paul Lalli, Jan 26, 2006
    #3
  4. Xicheng Guest

    wrote:
    > Hi,
    > I want to use regular expression to match a pattern which does not
    > have a whole word, let us say the word is "trash", then I tried this
    > pattern, [^trash], but it does not work. It sort of makes sense that
    > this does not work in that it does not treat "trash" as a whole word.

    that "[]" stuff matches a single char from the character class, not a
    whole word. you should take a look at negative look ahead (?!trash) or
    negative look behind(?<!trash) assertions.

    Xicheng
    Xicheng, Jan 26, 2006
    #4
  5. -bonn.de Guest

    > I want to use regular expression to match a pattern which does not
    > have a whole word, ...


    If I understand you well you are looking for something like this:

    @arr = qw(Use the force, Luke);
    $pattern = "uke";
    foreach $word (@arr){
    if( $word =~ m/$pattern/ && $word ne $pattern){
    print "Find pattern match, but it's not the whole word!\n";
    }
    }
    -bonn.de, Jan 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,272
  2. Replies:
    4
    Views:
    1,057
    Paul D
    Aug 26, 2006
  3. Guillermo Riojas
    Replies:
    5
    Views:
    200
    Guillermo Riojas
    Nov 22, 2010
  4. jm
    Replies:
    5
    Views:
    83
    Tad McClellan
    Nov 2, 2006
  5. Dylan Nicholson
    Replies:
    6
    Views:
    365
    A. Sinan Unur
    Oct 19, 2007
Loading...

Share This Page