Regexp problem

Discussion in 'Perl Misc' started by J Krugman, Aug 11, 2003.

  1. J Krugman

    J Krugman Guest

    Suppose I have some lengthy and/or complicated sub-regexp such as
    '(foo|bar|...)', how do I write a regexp that will match it, followed
    by some white space, followed by something that is neither foo or
    bar? I.e. I want this to match:

    bar frobozz

    but not this:

    bar foo

    TIA,

    Jill
    J Krugman, Aug 11, 2003
    #1
    1. Advertising

  2. J Krugman

    Sam Holden Guest

    On Mon, 11 Aug 2003 01:08:37 +0000 (UTC),
    J Krugman <> wrote:
    >
    >
    > Suppose I have some lengthy and/or complicated sub-regexp such as
    > '(foo|bar|...)', how do I write a regexp that will match it, followed
    > by some white space, followed by something that is neither foo or
    > bar? I.e. I want this to match:


    By translating your english into perl regular expression syntax:

    match it: (foo|bar|...)

    white space: \s+

    something: \S

    neither foo or bar : (?!foo|bar)

    The neither bit if the only not so common construct, see "perldoc perlre"
    for details.


    And putting it all together:

    /(foo|bar|...)\s+(?!foo|bar)\S/s


    I made a bunch of assumptions about what your words meant, things like
    "some" being 1 or more, "something" being 1 or more non-whitespace
    characters. If they are wrong, then the regex will be wrong.

    I also assumed you didn't want:

    "foo barn"

    to match, but did want:

    "foo seafood"

    to match. Again, if that's wrong the regex is wrong (but easily fixed).

    --
    Sam Holden
    Sam Holden, Aug 11, 2003
    #2
    1. Advertising

  3. J Krugman wrote:
    >
    > Suppose I have some lengthy and/or complicated sub-regexp such as
    > '(foo|bar|...)', how do I write a regexp that will match it, followed
    > by some white space, followed by something that is neither foo or
    > bar? I.e. I want this to match:
    >
    > bar frobozz
    >
    > but not this:
    >
    > bar foo


    my $first_part = qr/foo|bar|.../;

    my $regex = qr/$first_part \s+ (?!$first_part) \S/x;

    I'm not entirely sure how perl optomizes this, so you might want to
    prevent perl from backtracking over the \s+, as follows:

    my $regex = qr/$first_part (?>\s+) (?!$first_part) \S/x;

    This should only have a significant effect if you've got a string such
    as "bar foo" (with lots of whitespace in between the two
    parts). Without the (?>) it might take an excessive time to fail.

    --
    $a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
    );{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
    Benjamin Goldberg, Aug 12, 2003
    #3
    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. Greg Hurrell
    Replies:
    4
    Views:
    157
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    480
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    355
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    190
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    177
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page