Solved

Regular expression problem

Posted on 2004-03-25
38
542 Views
Last Modified: 2010-03-31
I want to use regular expression to validate a string is a number or not.
Here is my code:

    Pattern p = Pattern.compile("/\\d/");
    Matcher m = p.matcher(strNumber);
    bIsNullString = m.matches();
    System.out.println(strNumber+","+m.matches());

why m.matches() alway return false even the value of strNumber is "1", can you tell me why?

In javascript, I can use /\d*.{0,1}\d*/ pattern to fullfill my need.
Is it possible to implement the same thing in java?
When I execute the command "java -version"(-version parameter is used to print java version) on my pc, I got the following message:

D:\java>java -version
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)
0
Comment
Question by:cstsang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 10
  • 6
  • +1
38 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675284
Try "[0-9]+".
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675301
I haven't worked much with regex, but I guess in your code, it should be "\\d" ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675307
>> Try "[0-9]+"

You can also have "[0-9]*" but that would be for 0 or more matches - means that it can give true even when there are none. [0-9]+ would ensure at least one match.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:cstsang
ID: 10675469
First, I should thank you for your help.
My program works fine for integer only.
However, I want my program also allow the user input a decimal no.,so would you tell me how to do so?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675488
I guess a non-negative floating-point (or double) can be specified using:

"(\\d){1,10}\\.(\\d){1,10}"
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675630
I assumed a size-limit there.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10675832
The reason for your difficultly lies in the way you've specified your pattern. In some scripting languages, including Perl and JavaScript, the pattern is contained the forward slashes

/somePattern/

This is not only unnecessary in Java, but will break it. It should be just

somePattern

(in your case "\\d"). Be careful also with the distinction between Matcher.matches and Matcher.find. The former must match ALL the input
0
 
LVL 7

Expert Comment

by:searlas
ID: 10676916
Taking on board all the comments so far, and adding an additional assumption that you want to match the entire string.
i.e. "23" should match, but "this contains 23" should not:
Use \A to anchor the start of the string, \z to anchor the end.  The regular expression is contained within quotes, with no need for '/' delimiters.

Pattern p = Pattern.compile("\\A\\d+\\z");

If you want "23", "23.2" "0.43" but not ".2" or "20." to match:
Pattern p = Pattern.compile("\\A\\d+(?:\\.\\d+)\\z");

And, if you want to allow a leading + or -:
Pattern p = Pattern.compile("\\A[-+]\\d+(?:\\.\\d+)\\z");
0
 
LVL 7

Expert Comment

by:searlas
ID: 10676932
There's always a typo...

Non-capturing fractional part "(?:....)" needs to be option by adding a trailing ?
If you want "23", "23.2" "0.43" but not ".2" or "20." to match:
Pattern p = Pattern.compile("\\A\\d+(?:\\.\\d+)?\\z");

And, if you want to allow a leading + or -:
Pattern p = Pattern.compile("\\A[-+]\\d+(?:\\.\\d+)?\\z");
0
 
LVL 7

Author Comment

by:cstsang
ID: 10683395
Hello everybody:
In fact, I am writing "number to text" function.
do you think whether I need to take care the "-" sign?

I found that the pattern [0-9]*\\.{0,1}[0-9]* can fullfill my need, when I don't take care the "-" sign.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10683412
You probably do need to take care of it then
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10684430
Perhaps: "-*[0-9]*\\.{0,1}[0-9]*" ??
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10684452
Sorry, perhaps: "-??[0-9]*\\.{0,1}[0-9]*"
0
 
LVL 7

Author Comment

by:cstsang
ID: 10684480
how about this one?

-{0,1}[0-9]*\\.{0,1}[0-9]*
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10684653
Should work.
0
 
LVL 7

Expert Comment

by:searlas
ID: 10685775
Isn't it standard practice to use ? instead of {0,1}.
I'd have thought people reading regular expressions parse ? mentally as 'optional', where as it takes a bit of thought to realise that {0,1} is doing exactly the same thing...

So:
-{0,1}[0-9]*\\.{0,1}[0-9]*

becomes
-?[0-9]*\\.?[0-9]*

But, that accepts "", "-", "-." and "." (as does the expression with {0,1})


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10685801
>>Isn't it standard practice to use ? instead of {0,1}.

Yes, and much better of course

Not sure if i get your last sentence searlas...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10685882
>> Isn't it standard practice to use ? instead of {0,1}

Yes, it is. That's why I used -?? (zero or one appearance of - ).

If we assume that a 0 or at least any other digit is essential before the decimal-point, then we can have:

"-??[0-9]+\\.??[0-9]*"
0
 
LVL 7

Expert Comment

by:searlas
ID: 10685958
Why use a reluctant qualifier? (double ??)

I'm just aiming for clarity here and consistency with the bare essentials of regular expressions.  I think a reluctant qualifier is only necessary when it could cause a regular expression to fail e.g.
[0-9]?[0-9]
This would fail to match "0", "1", "2", ....
But,
[0-9]??[0-9] would match "0", "1", "2", ...

However, as you're qualifying a "-" and a "." which have no bearing in whether decimal numbers match, there seems to be no point in using a reluctant qualifier, and it causes the reader/maintainer to double-check Java's regular expression spec to see what the double ?? actually means.

Well, it caused me to read the Pattern javadocs.  Unnecessarily.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10685968
LOL me too - always a good read though ;-)
0
 
LVL 7

Author Comment

by:cstsang
ID: 10685994
I want to ensure that at most 1 decimal point in the string.
I want to ensure that at most 1 negative sign"-" in the string.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10686006
(implemented ;-))
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 50 total points
ID: 10686060
Use any of the ones described above:

>> "-??[0-9]+\\.??[0-9]*"
>> "-{0,1}[0-9]*\\.{0,1}[0-9]*"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10686068
Wait a minute mayank - haven't those been rejected for the reasons we've been discussing..?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10686111
cstang:

a. that particular answer (*at that point in the thread*) should not have been accepted as the answer for the reasons just given
b. both mayank and searlas have been refining this answer (i've just been chipping in) and BOTH of them should have been credited in a points split

I'm not getting at you mayank of course, just trying to see a precise answer coming out of all this work that's gone in
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10686112
All would work, though. I still don't favour or reject any one of them.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10686121
>> should have been credited in a points split

No issues with that.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10686130
You can post a question "Points for searlas" and give him points there. Or if you want to go the long way, you can ask CS to reopen this one and then split.
0
 
LVL 7

Expert Comment

by:searlas
ID: 10686346
cstang,

Do not use  "-{0,1}[0-9]*\\.{0,1}[0-9]*"
(Nor the alternatives with ? or ??)
This expression matches the empty string, "-", "." and "-." on their own.  i.e. no numbers at all.  As stated in this comment.
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20931464.html#10675307

And this: "-??[0-9]+\\.??[0-9]*"
It depends on your requirements, but you should know it'll accept numbers with a trailing decimal point, but not numbers with a leading decimal point.  i.e. it accepts "23." and "-23."  but not ".23" or "-.23"

With respect to points, I'd just be happy if cstang uses the simplest/clearest pattern that meets their requirements.  
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10686593
>> "-??[0-9]+\\.??[0-9]*"

Oh sorry. I did not notice the * at the end.

Perhaps if you restrict it to something like (+/-) x.y, then you can have: "-??[0-9]+\\.??[0-9]+"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10686822
That's still of course ignoring the earlier remarks about greedy quantifiers
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10686840
Yes.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10686861
I haven't worked much with regex, like I pointed right at the start, so as of now, for me - as long as it works, its good ;-) will work with it more.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10687045
To summarize (cstang: searlas is happy - you can forget the points split)

Tweaking :


>>Perhaps if you restrict it to something like (+/-) x.y, then you can have: "-?[0-9]+\\.?[0-9]+"

That looks good and simple to me
0
 
LVL 7

Author Comment

by:cstsang
ID: 10687538
I have try the following pattern:

"-??[0-9]+\\.??[0-9]*"

It can filter out single "-" and ".". Also it can also allow user input "1.". Therefore, I select this comment as answer.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10687565
I give up now LOL
0
 
LVL 7

Expert Comment

by:searlas
ID: 10687811
OK.  I'm curious what you meant when you said:
    In fact, I am writing "number to text" function.

You do know your chosen pattern will also allow user input "R2D2" and "C3PO" don't you?

0
 
LVL 7

Author Comment

by:cstsang
ID: 10691684
hey searlas:

I have tested the pattern, it can filter out both "R2D2" and "C3PO"
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 93
Bot application - advice 3 79
jsp CRUD operations with and without prepared statement also hibernatge 1 48
Java regex 5 19
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question