regex debugging: alphanumeric with dashes

E

enrique

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.
 
L

Lasse Reichstein Nielsen

enrique said:
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
 
S

SMC

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top