Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

regex debugging: alphanumeric with dashes

Thread view: 
enrique - 18 May 2005 22:49 GMT
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.
Lasse Reichstein Nielsen - 18 May 2005 23:33 GMT
> 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
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

SMC - 19 May 2005 04:27 GMT
> 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
[quoted text clipped - 27 lines]
>
> 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?

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.