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 / November 2006

Tip: Looking for answers? Try searching our database.

Regular Expression

Thread view: 
the87boy@gmail.com - 09 Nov 2006 13:23 GMT
Why do this regular expression not work:
if (str.matches("^\\d*$") || str.matches("^\\d*" + dot + "$") ||
str.matches("^\\d*" + dot + "\\d*$")) {
return true;
} else {
return false;
}

dot is a string, who looks like this:
String dot = ",";

The 1st regular expression test if it is just a number
The 2nd regular expression test if it is a number and ends with a dot
The 3rd regular exression test if it is a dot sorrunded by numbers
It's only number (can be with a dot)

Can somebody tell me, what's wrong and maybe shorten it?
Jussi Piitulainen - 09 Nov 2006 14:22 GMT
> Why do this regular expression not work:
> if (str.matches("^\\d*$") || str.matches("^\\d*" + dot + "$") ||
[quoted text clipped - 13 lines]
>
> Can somebody tell me, what's wrong and maybe shorten it?

It appears to work. It matches ",14" and "3," and "," in addition to
"314" and "3,14", though. You failed to give an example of a str that
you think should match and does not, or that you think should not
match and does.

"foo|bar|baz" matches "foo" or "bar" or "baz".
"foo,?bar" matches "foobar" or "foo,bar".

The anchors are redundant with .matches().


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.