Regex - disincluding strings in a match

Discussion in 'Perl Misc' started by Steve Chiang, May 27, 2005.

  1. Steve Chiang

    Steve Chiang Guest

    Hello,

    How would one disinclue a string in a match? For example, I want to match
    the next animal that comes after "dog", but don't want to match "dog":

    cat cow dog monkey zebra

    .... so I would want to match only "monkey". However, the order of the items
    in the list may change anytime:

    zebra monkey dog cow cat

    .... so I would want to match only "cow" in this case since it follows "dog"
    directly. Further, I am limited in that I cannot use back-references for
    this.

    Thanks,
    Steve
    Steve Chiang, May 27, 2005
    #1
    1. Advertising

  2. "Steve Chiang" <> writes:
    > How would one disinclue a string in a match? For example, I want to match
    > the next animal that comes after "dog", but don't want to match "dog":
    >
    > cat cow dog monkey zebra


    my $string = "cat cow dog monkey zebra";
    my $animal = "dog";
    my (undef, $match) = $string =~ /(\Q$animal\E) (?!\1)(\w+)/;
    print "Match: [$match]\n";

    Look up "negative lookahead" in perlre for more details.

    > ... so I would want to match only "cow" in this case since it follows "dog"
    > directly. Further, I am limited in that I cannot use back-references for
    > this.


    Why not? Perl supports backreferences. If you're trying to do this
    in a non-Perl language that supports regexes, then you'd best ask
    there; regexes very a LOT between languages, and the only way to know
    for sure is to check the docs for whatever language you're using.

    Anyway, I'm sure you can see how to avoid backreferences in that code;
    I'll leave eliminating them as an exercise for the reader.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, May 27, 2005
    #2
    1. Advertising

  3. Steve Chiang

    Guest

    Eric Schwartz wrote:
    > "Steve Chiang" <> writes:
    > > How would one disinclue a string in a match? For example, I want to match
    > > the next animal that comes after "dog", but don't want to match "dog":
    > >
    > > cat cow dog monkey zebra

    >
    > my $string = "cat cow dog monkey zebra";
    > my $animal = "dog";
    > my (undef, $match) = $string =~ /(\Q$animal\E) (?!\1)(\w+)/;
    > print "Match: [$match]\n";
    >


    Why not simply:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $string = "cat cow dog monkey zebra";

    $string =~ /dog\s*(\w+)/;

    print $1;

    I know I am not really matching "the word after dog" exactly, but the
    outcome is the same I think.

    wana
    , May 27, 2005
    #3
  4. writes:
    > Eric Schwartz wrote:
    >> "Steve Chiang" <> writes:
    >> > How would one disinclue a string in a match? For example, I want to match
    >> > the next animal that comes after "dog", but don't want to match "dog":

    ^^^^^^^^^^^^^^^^^^^^^^^^^
    > Why not simply:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my $string = "cat cow dog monkey zebra";
    >
    > $string =~ /dog\s*(\w+)/;


    That matches "dog dog";

    > print $1;


    My solution was just a touch more flexible than required (i.e., I made
    the animal to look for variable), but definitely won't match "dog
    dog" (assuming $animal eq "dog").

    > I know I am not really matching "the word after dog" exactly, but the
    > outcome is the same I think.


    Actually, that's exactly what you are matching. It's just not what
    the OP asked for.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, May 27, 2005
    #4
  5. * Steve Chiang schrieb:
    >
    > How would one disinclue a string in a match? For example, I want to match
    > the next animal that comes after "dog", but don't want to match "dog":
    >
    > cat cow dog monkey zebra
    >
    > ... so I would want to match only "monkey".


    > Further, I am limited in that I cannot use back-references for
    > this.


    You could use a technique called "zero-width positive look-behind" and
    is described in `perldoc perlre`. Unless you have perl installed on your
    system read this document at <http://perldoc.perl.org/perlre.html>.

    my $string = "cat cow dog monkey zebra";
    print $string =~ m/(?<=dog\s)\S*/g;
    __END__
    monkey

    regards,
    fabian
    Fabian Pilkowski, May 27, 2005
    #5
  6. On 2005-05-27, Steve Chiang scribbled these
    curious markings:
    > Further, I am limited in that I cannot use back-references for this.


    Then perhaps you should ask your teacher for help if you can't figure it
    out on your own.

    Best Regards,
    Christopher Nehren
    --
    I abhor a system designed for the "user", if that word is a coded
    pejorative meaning "stupid and unsophisticated". -- Ken Thompson
    If you ask the wrong people questions, you get "Joel on Software".
    Unix is user friendly. However, it isn't idiot friendly.
    Christopher Nehren, May 27, 2005
    #6
  7. Steve Chiang

    Guest

    "Steve Chiang" <> wrote:
    > Hello,
    >
    > How would one disinclue a string in a match? For example, I want to
    > match the next animal that comes after "dog", but don't want to match
    > "dog":


    You either match the string, or you do not match the string. You cannot
    match part of a string. You can *capture* part of a string. Is that
    what you meant to ask?

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , May 27, 2005
    #7
  8. <> wrote:
    > Eric Schwartz wrote:
    >> "Steve Chiang" <> writes:
    >> > I want to match
    >> > the next animal that comes after "dog", but don't want to match "dog":


    [snip Eric's code]

    > Why not simply:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my $string = "cat cow dog monkey zebra";
    >
    > $string =~ /dog\s*(\w+)/;
    >
    > print $1;



    You should never use the dollar-digit variables unless you
    have first ensured that the match *succeeded*.

    print $1 if $string =~ /dog\s*(\w+)/;


    > I know I am not really matching "the word after dog" exactly, but the
    > outcome is the same I think.



    What does it match when you try it with

    $string = "cat cow dogma monkey zebra";

    ??


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 28, 2005
    #8
  9. Steve Chiang

    Eric Amick Guest

    On Fri, 27 May 2005 23:11:17 +0200, Fabian Pilkowski
    <-marburg.de> wrote:

    >* Steve Chiang schrieb:
    >>
    >> How would one disinclue a string in a match? For example, I want to match
    >> the next animal that comes after "dog", but don't want to match "dog":
    >>
    >> cat cow dog monkey zebra
    >>
    >> ... so I would want to match only "monkey".

    >
    >> Further, I am limited in that I cannot use back-references for
    >> this.

    >
    >You could use a technique called "zero-width positive look-behind" and
    >is described in `perldoc perlre`. Unless you have perl installed on your
    >system read this document at <http://perldoc.perl.org/perlre.html>.
    >
    > my $string = "cat cow dog monkey zebra";
    > print $string =~ m/(?<=dog\s)\S*/g;
    > __END__
    > monkey


    Almost--that will match the second "dog" in "dog dog". What you need is

    print $string =~ m/(?<=dog\s)(?!dog)\S+/g;

    --
    Eric Amick
    Columbia, MD
    Eric Amick, May 28, 2005
    #9
    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. hiwa
    Replies:
    0
    Views:
    635
  2. Replies:
    3
    Views:
    744
    Reedick, Andrew
    Jul 1, 2008
  3. Replies:
    8
    Views:
    2,680
    Lie Ryan
    Jun 14, 2009
  4. John Butler

    regex to match similar strings

    John Butler, Jul 12, 2010, in forum: Ruby
    Replies:
    2
    Views:
    220
    John Butler
    Jul 12, 2010
  5. Dave
    Replies:
    8
    Views:
    150
    Dave Weaver
    Aug 10, 2005
Loading...

Share This Page