Solved

Regular expression problem

Posted on 2004-03-25
38
534 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
  • 16
  • 10
  • 6
  • +1
38 Comments
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Try "[0-9]+".
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
I haven't worked much with regex, but I guess in your code, it should be "\\d" ?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
 
LVL 7

Author Comment

by:cstsang
Comment Utility
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:mayankeagle
Comment Utility
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:mayankeagle
Comment Utility
I assumed a size-limit there.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You probably do need to take care of it then
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Perhaps: "-*[0-9]*\\.{0,1}[0-9]*" ??
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Sorry, perhaps: "-??[0-9]*\\.{0,1}[0-9]*"
0
 
LVL 7

Author Comment

by:cstsang
Comment Utility
how about this one?

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

Expert Comment

by:mayankeagle
Comment Utility
Should work.
0
 
LVL 7

Expert Comment

by:searlas
Comment Utility
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
Comment Utility
>>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:mayankeagle
Comment Utility
>> 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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

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

Author Comment

by:cstsang
Comment Utility
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
Comment Utility
(implemented ;-))
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 50 total points
Comment Utility
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
Comment Utility
Wait a minute mayank - haven't those been rejected for the reasons we've been discussing..?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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:mayankeagle
Comment Utility
All would work, though. I still don't favour or reject any one of them.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> should have been credited in a points split

No issues with that.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> "-??[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
Comment Utility
That's still of course ignoring the earlier remarks about greedy quantifiers
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Yes.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I give up now LOL
0
 
LVL 7

Expert Comment

by:searlas
Comment Utility
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
Comment Utility
hey searlas:

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now