Regular Expression Help Needed

Discussion in 'Perl Misc' started by Deja User, Feb 11, 2004.

  1. Deja User

    Deja User Guest

    I am new to regular expressions and I can't seem to figure out
    following.

    (1) Regular expression should find a match, if the given string does
    NOT start with 90, 91, 31, or 32.

    (2) Find a match, if the string contains DNI or 'DO NOT INSTALL'
    anywhere in the string.

    I need two seperate expressions for (1) and (2).

    Thanks
     
    Deja User, Feb 11, 2004
    #1
    1. Advertising

  2. Didn't you find any further newsgroups where you could post your
    homework questions?

    Deja User wrote:
    > I am new to regular expressions and I can't seem to figure out
    > following.
    >
    > (1) Regular expression should find a match, if the given string
    > does NOT start with 90, 91, 31, or 32.
    >
    > (2) Find a match, if the string contains DNI or 'DO NOT INSTALL'
    > anywhere in the string.
    >
    > I need two seperate expressions for (1) and (2).


    What have you tried so far?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #2
    1. Advertising

  3. Deja User

    Paul Lalli Guest

    > Date: 11 Feb 2004 08:13:13 -0800
    > From: Deja User <>
    > Newsgroups: comp.lang.perl.misc, comp.lang.tcl, comp.unix.questions, alt.php,
    > comp.programming
    > Subject: Regular Expression Help Needed
    >
    > I am new to regular expressions and I can't seem to figure out
    > following.
    >
    > (1) Regular expression should find a match, if the given string does
    > NOT start with 90, 91, 31, or 32.
    >
    > (2) Find a match, if the string contains DNI or 'DO NOT INSTALL'
    > anywhere in the string.
    >
    > I need two seperate expressions for (1) and (2).
    >
    > Thanks
    >


    Well, I have no idea what all those other groups will give you, but here
    you'll get a perl answer. And btw, this sounds suspciously like a
    homework question.... naughty naughty.

    $string !~ /^(90|91|31|32)/

    $string =~ /(DNI|DO NOT INSTALL)/

    Paul Lalli
     
    Paul Lalli, Feb 11, 2004
    #3
  4. Deja User wrote:

    > I am new to regular expressions and I can't seem to figure out
    > following.
    >

    this will help learn about REs

    http://www.tcl.tk/man/tcl8.4/TclCmd/re_syntax.htm
    > (1) Regular expression should find a match, if the given string does
    > NOT start with 90, 91, 31, or 32.
    >

    a negative lookahead constraint is what you want:

    http://www.tcl.tk/man/tcl8.4/TclCmd/re_syntax.htm#M26

    regexp {^(?!(90|91|31|32))} $input

    > (2) Find a match, if the string contains DNI or 'DO NOT INSTALL'
    > anywhere in the string.
    >

    this is simple alternation

    regexp {DNI|NO NOT INSTALL} $input


    > I need two seperate expressions for (1) and (2).
    >
    > Thanks


    Bruce
     
    Bruce Hartweg, Feb 11, 2004
    #4
  5. Deja User

    Deja User Guest

    Thanks for the help. Just to clear things up, this is NOT a homework
    question.. I already graduated from collge in 94. I am just new to
    regular expressions. I already did some research on the web and read
    some tutorials.. but still having trouble.
     
    Deja User, Feb 12, 2004
    #5
  6. Deja User

    Deja User Guest

    Here is my input and I am testing it in UltraEdit.. but your
    suggestions don't seem to work. Am I doing anything wrong? I was
    hoping that UltraEdit would highlight the matched lines.

    INPUT
    -----

    32000806-001
    91000883-xxx
    64000343-394
    20032043-001
    31091003-003
    90384334-092
    10903103-033
    20932002-934
    This is my cat
    cat is good
    Test Point, DNI
    TP, DO NOT INSTALL
    DNI
    34000000-343
     
    Deja User, Feb 12, 2004
    #6
  7. Deja User wrote:

    > I am new to regular expressions and I can't seem to figure out
    > following.
    >
    > (1) Regular expression should find a match, if the given string
    > does NOT start with 90, 91, 31, or 32.


    A pity about that "NOT". To find strings that match, you can just
    do:
    ^((3[12])|(9[01])).*$

    This is worth exploring if you're not too familiar with REs.

    ^((3[12])|(9[01])).*$ - the whole thing
    ^ - anchors RE to start of line
    ( ) - groups the two possible sub-patterns
    ( )|( ) - sub-pattern A *OR* sub-pattern B
    3[12] - a "3" followed by a "1" or "2"
    9[10] - a "9" followed by a "0" or "1"
    .* - anything (i.e. rest of the line)
    $ - anchors RE to end of line

    NOTE: some systems require you to escape the grouping parentheses
    and the "either/or" delimiter:

    ^\(\(3[12]\)\|\(9[01]\)\).*$

    But the combination of the NOT and the double-digit prefixes makes
    it a little harder. Something like this, perhaps:

    ^((9[^01])|(3[^12])|[^39]).*$

    Broken out a bit:

    ^( (9[^01]) | (3[^12]) | [^39] ) .* $

    This works by allowing lines that don't match.

    ^((9[^01])|(3[^12])|[^39]).*$ -
    ^( | | ).*$ - SOL, prefix, rest of line, EOL
    (9[^01]) - "9" + NOT "0" or "1"
    (3[^12]) - "3" + NOT "1" or "2"
    [^39] - anything BUT NOT "3" or "9"

    > (2) Find a match, if the string contains DNI or 'DO NOT INSTALL'
    > anywhere in the string.


    Easy:
    (DNI)|(DO NOT INSTALL)


    --
    |_ CJSonnack <> _____________| How's my programming? |
    |_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL |
    |_____________________________________________|_______________________|
     
    Programmer Dude, Feb 12, 2004
    #7
  8. Deja User wrote:
    > Here is my input and I am testing it in UltraEdit.. but your
    > suggestions don't seem to work. Am I doing anything wrong? I was
    > hoping that UltraEdit would highlight the matched lines.
    >
    > INPUT
    > -----
    >
    > 32000806-001
    > 91000883-xxx
    > 64000343-394
    > 20032043-001
    > 31091003-003
    > 90384334-092
    > 10903103-033
    > 20932002-934
    > This is my cat
    > cat is good
    > Test Point, DNI
    > TP, DO NOT INSTALL
    > DNI
    > 34000000-343


    There was a typo in the 2nd RE (had NO instead of DO) but other than that
    the REs work (run the following:)

    set RE1 {^(?!(90|91|31|32))}
    set RE2 {DNI|DO NOT INSTALL}

    set input {
    32000806-001
    91000883-xxx
    64000343-394
    20032043-001
    31091003-003
    90384334-092
    10903103-033
    20932002-934
    This is my cat
    cat is good
    Test Point, DNI
    TP, DO NOT INSTALL
    DNI
    34000000-343
    }

    foreach line [split $data \n] {
    puts "Checking >> $line << :"
    if {[regexp $RE1 $line]} {puts " Does NOT start with 90,91,31 or 32"}
    if {[regexp $RE2 $line]} {puts " Contains DNI or DO NOT INSTALL"}
    puts ""
    }

    Note that this is Tcl (since thats the newsgroup I read your question on)
    and I don;t know Ultraedit, so the RE syntax could be slightly different
    (as well as supoort for negative lookaheads) - so either the REs need
    tweaked for Ultraedit, or there is something else in the settings beyaond
    the RE for UltraEdit to highlight.

    Bruce
     
    Bruce Hartweg, Feb 12, 2004
    #8
  9. In article <>,
    (Deja User) wrote:

    > Here is my input and I am testing it in UltraEdit.. but your
    > suggestions don't seem to work. Am I doing anything wrong? I was
    > hoping that UltraEdit would highlight the matched lines.


    Different programs use different regular expression matchers, and they
    often have different advanced features. I believe the suggestions were
    intended for TCL, since you cross-posted to comp.lang.tcl.

    Which language are you really planning to use? Post just to that group,
    and you should get an answer most appropriate to your needs.

    --
    Barry Margolin,
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
     
    Barry Margolin, Feb 12, 2004
    #9
  10. Deja User wrote:
    >
    > Thanks for the help. Just to clear things up, this is NOT a homework
    > question.. I already graduated from collge in 94. I am just new to
    > regular expressions. I already did some research on the web and read
    > some tutorials.. but still having trouble.


    Your best bet is to get the book Mastering Regular Expressions.
    http://www.oreilly.com/catalog/regex2/index.html


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Feb 13, 2004
    #10
  11. Deja User

    Deja User Guest

    Thanks for very detailed explanation... There is a 3rd party
    application I am using that filters out unwanted lines. It uses GNU
    regular expression engine.
     
    Deja User, Feb 13, 2004
    #11
    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,303
  2. David Cho
    Replies:
    4
    Views:
    402
    David Cho
    Feb 15, 2005
  3. Chanchal
    Replies:
    1
    Views:
    495
    NullBock
    Dec 13, 2005
  4. pekka niiranen
    Replies:
    5
    Views:
    525
    Paul McGuire
    Oct 20, 2004
  5. ragz_82
    Replies:
    1
    Views:
    300
    ragz_82
    Aug 4, 2009
Loading...

Share This Page