regex debugging: alphanumeric with dashes

Discussion in 'Java' started by enrique, May 18, 2005.

  1. enrique

    enrique Guest

    Boy, regular expressions almost deserves its own topic!

    I'm trying to debug my expression that matches an alphanumeric with any
    number of dashes (including none), except at the ends and obviously
    disallowing two or more consecutive dashes.

    Here it is: /\w+(-?\w+)*/

    Test cases that I expect to pass are failing:

    "01234abcdef" true

    "0123-412-8370" false (should've been "true")

    "asdkfjakfj" true

    "0-1" false (should've been "true")

    "-" false

    "--" false

    "-ABC123" false

    "00230-" false

    "ABC-123" false (should've been "true")

    "1-" false

    "111223333" true

    Would anyone lend a hand? Thank you.
     
    enrique, May 18, 2005
    #1
    1. Advertising

  2. "enrique" <> writes:

    > I'm trying to debug my expression that matches an alphanumeric with any
    > number of dashes (including none), except at the ends and obviously
    > disallowing two or more consecutive dashes.
    >
    > Here it is: /\w+(-?\w+)*/


    Looks fine. You probably want to anchor it, otherwise it should give
    true for any string containing a match for \w. Also, the "?" is not
    necessary:
    /^\w+(-\w+)*/

    > "0123-412-8370" false (should've been "true")


    What are you doing? Because it gives true for me.

    > "-ABC123" false


    Should be true for the RegExp you gave.

    > "00230-" false


    Ditto.

    > "ABC-123" false (should've been "true")


    Is true.

    > Would anyone lend a hand?


    You are doing *something* wrong, because it works as expected for me.

    Can you show us the page that fails, and say which browser you are
    using?

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, May 18, 2005
    #2
    1. Advertising

  3. enrique

    SMC Guest

    On Thu, 19 May 2005 07:49:07 +1000, enrique wrote:

    > Boy, regular expressions almost deserves its own topic!


    Actually, several books in fact ;-)

    > I'm trying to debug my expression that matches an alphanumeric with any
    > number of dashes (including none), except at the ends and obviously
    > disallowing two or more consecutive dashes.
    >
    > Here it is: /\w+(-?\w+)*/
    >
    > Test cases that I expect to pass are failing:
    >
    > "01234abcdef" true
    >
    > "0123-412-8370" false (should've been "true")
    >
    > "asdkfjakfj" true
    >
    > "0-1" false (should've been "true")
    >
    > "-" false
    >
    > "--" false
    >
    > "-ABC123" false
    >
    > "00230-" false
    >
    > "ABC-123" false (should've been "true")
    >
    > "1-" false
    >
    > "111223333" true
    >
    > Would anyone lend a hand? Thank you.


    I tried your test cases with the following ugly test program

    import java.util.regex.*;

    public class t {

    public static void main (String[] args){
    if (args[0].matches(args[1]))
    System.out.println("matches");
    else
    System.out.println("does not match");
    }
    }

    Results were:

    [sean@se2 tmp]$ java t 0123-412-8370 '\w+(-?\w+)*' matches

    [sean@se2 tmp]$ java t 0-1 '\w+(-?\w+)*' matches

    [sean@se2 tmp]$ java t ABC-123 '\w+(-?\w+)*' matches

    Which according to your post match correctly as expected, so I guess your
    regex is ok. Want to post
    your code snippet for examination?

    --
    Sean

    "It is one of the essential features of [incompetence] that the person
    so afflicted is incapable of knowing that he is incompetent. To have
    such knowledge would already be to remedy a good portion of the
    offense." --W.I. Miller
     
    SMC, May 19, 2005
    #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. Chris Bedford
    Replies:
    0
    Views:
    438
    Chris Bedford
    Aug 17, 2003
  2. Chris Bedford
    Replies:
    0
    Views:
    550
    Chris Bedford
    Aug 17, 2003
  3. Paul
    Replies:
    2
    Views:
    397
  4. Paul

    Odd dashes

    Paul, Jun 26, 2006, in forum: HTML
    Replies:
    3
    Views:
    500
  5. enrique
    Replies:
    14
    Views:
    254
    Dr John Stockton
    May 23, 2005
Loading...

Share This Page