RegEx How to not match a string

Discussion in 'Perl Misc' started by jim_adams@hotmail.com, Oct 3, 2005.

  1. Guest

    I would like to match a string between AB that does not contain the
    word "bad" and contains upto 5 characters ending in ";"

    This is what I have so far, but it does not work. It does not group
    the string "BAD" and instead matches any string that has "B", "A" or
    "D" in it.
    A(?<TAG>(([^;^(BAD)]{0,5})))B

    For "ADDDDDB", <TAG> should contain "DDDDD"
    For "AZZ;XXB", <TAG> should contain "ZZ;"
    For "ABADB", <TAG> should be empty

    Thanks,
    Jim
    , Oct 3, 2005
    #1
    1. Advertising

  2. Dr.Ruud Guest

    :

    > I would like to match a string between AB that does not contain the
    > word "bad" and contains upto 5 characters ending in ";"
    >
    > This is what I have so far, but it does not work. It does not group
    > the string "BAD" and instead matches any string that has "B", "A" or
    > "D" in it.
    > A(?<TAG>(([^;^(BAD)]{0,5})))B
    >
    > For "ADDDDDB", <TAG> should contain "DDDDD"
    > For "AZZ;XXB", <TAG> should contain "ZZ;"
    > For "ABADB", <TAG> should be empty


    Do it in 2 steps, step 1 being to remove any internal 'BAD'.

    And anchor the starting A and ending B.

    Step 2: s/^A(([^;]){1,4}[\2;]?).*B$/$1/

    The [\2;] can also be written as (?:\2|;).

    (untested)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Oct 4, 2005
    #2
    1. Advertising

  3. Matt Garrish Guest

    <> wrote in message
    news:...
    >I would like to match a string between AB that does not contain the
    > word "bad" and contains upto 5 characters ending in ";"
    >
    > This is what I have so far, but it does not work. It does not group
    > the string "BAD" and instead matches any string that has "B", "A" or
    > "D" in it.
    > A(?<TAG>(([^;^(BAD)]{0,5})))B
    >


    That looks suspiciously like a .Net regular expression. It certainly isn't
    Perl. All I can tell you is that you can't use a character class like that
    (character classes are for characters, oddly enough). If you were using Perl
    I would suggest you look up negative lookahead assertions in perlre. As I
    doubt you are, you should try a group that specializes in whatever language
    you're using.

    Matt
    Matt Garrish, Oct 4, 2005
    #3
  4. <> wrote:

    > For "ADDDDDB", <TAG> should contain "DDDDD"



    Variables in Perl start with a dollar sign.

    <TAG> cannot "contain" anything.

    What programming language are you using?

    Try posting actual Perl code *that we can run*.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 4, 2005
    #4
  5. <> wrote:
    > I would like to match a string between AB that does not contain the
    > word "bad" and contains upto 5 characters ending in ";"
    >
    > This is what I have so far, but it does not work.



    Of course not, it does not even compile!


    > It does not group
    > the string "BAD" and instead matches



    It can never match anything if it cannot even execute!

    > any string that has "B", "A" or
    > "D" in it.



    That is what you told it to do.

    If that isn't what you want, then tell it to do something else.


    > A(?<TAG>(([^;^(BAD)]{0,5})))B
    >
    > For "ADDDDDB", <TAG> should contain "DDDDD"
    > For "AZZ;XXB", <TAG> should contain "ZZ;"
    > For "ABADB", <TAG> should be empty



    ------------------------
    while ( <DATA> ) {
    chomp;
    foreach my $match ( /A([^;]{0,5};?).*B/g ) {
    print "$match\n" unless $match =~ /bad/i;
    }
    }

    __DATA__
    ADDDDDB
    AZZ;XXB
    ABADB
    ------------------------


    > Thanks,



    Your stealthyness has earned you a spot in the scorefile. So long.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 4, 2005
    #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. hiwa
    Replies:
    0
    Views:
    623
  2. Replies:
    3
    Views:
    716
    Reedick, Andrew
    Jul 1, 2008
  3. Neil
    Replies:
    3
    Views:
    1,024
    Andreas Leitgeb
    Aug 29, 2009
  4. Old Echo
    Replies:
    1
    Views:
    170
    Adam Shelly
    Sep 4, 2008
  5. Ruby Newbee

    regex =~ string or string =~ regex?

    Ruby Newbee, Jan 4, 2010, in forum: Ruby
    Replies:
    3
    Views:
    121
    Kirk Haines
    Jan 4, 2010
Loading...

Share This Page