File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to find if there is a number in a string using regular expressions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find if there is a number in a string using regular expressions." Watch "How to find if there is a number in a string using regular expressions." New topic
Author

How to find if there is a number in a string using regular expressions.

Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40
Hi,

I need to validate the string that it should contain atleast one number.

How can I validate this using regular expressions. I have tried some examples but it is not working.

String candidate = "Java 6";
String pattern = "\\d";
if(candidate.matches(pattern)) {
valid
else {
invalid
}


Thanks,
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What your code checks is whether the string matches the pattern "exactly one digit". That's obviously not what you want.

One way to reword "contains a number" is "matches an arbitrary number of arbitrary characters, followed by a digit, followed by an arbitrary number of arbitrary characters.

Does that give you an idea on how to translate that in a regular expression?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40
I need that if user gives a word it should contain atleast one number in any position, the string must contain atleaset one number.

some examples

these are valid:

india5country

4rfdgfrdd

these are invalid:

asdfasfd

asdfasdf

because these strings doesn't contain a number.
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
String.matches() doesn't look for a substring to match the pattern, but checks whether the whole string matches the pattern. You will see that "6".matches("\\d") returns true but "x6".matches("\\d") returns false.

You need to create a Pattern object and use the find() function. Take a look at the documentation, it is pretty clear.
[ October 29, 2007: Message edited by: Jan van Mansum ]

SCJP 1.4, SCWCD 1.4
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
I wouldn't use a Pattern object as Jan suggested. You are on the right track, but as Ilja said, your pattern is not good enough.
Currently you have "\\d" which translates to "string is exactly one digit", which is obvious not what you want.
You want "string contains a number", which is the same as "string contains any number of characters, followed by a digit, followed by any number of characters", which would result in a slightly more complex pattern "xxx\\dxxx" where xxx translates into "any number of characters". Take a look at the mentionned documentation to see how to replace the xxx part.
Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40
I have given

Pattern pat=Pattern.compile("[a-zA-Z0-9]");
Matcher matcher=pat.matcher(username);

if(matcher.find()) {
valid = true;
}

It is not working
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74


Works fine for me.

I think you first need to decide what you are looking for. First you were looking for a single digit, now you are trying to find one of: lowercase letter, uppercase letter, digit.

Maybe it is not some much the Java as it is the regular expressions that are difficult or unfamiliar to you. There is plenty of resources about that on the internet, just type "regular expression tutorial" in Google.

Good luck!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

To be honest, if your check is just something simple as "are there at least X numbers" or "are there at least X occurrances of some character Y", just iterating would probably be faster:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

I wouldn't use a Pattern object as Jan suggested. You are on the right track, but as Ilja said, your pattern is not good enough.


I also agree. There is no reason to change the algorithm, when it is just a pattern string that needs to be changed.

Anyway, since Jan gave the solution using find(), I highly doubt the OP will continue investigating here. So here is what Ilja meant...

One way to reword "contains a number" is "matches an arbitrary number of arbitrary characters, followed by a digit, followed by an arbitrary number of arbitrary characters.


means...



Nothing else needs to change.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40
If I give like

String username = "Something #$%^&at 3 contains a number.";


it is printing as valid. special characters are not there in regular expression.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

it is printing as valid. special characters are not there in regular expression.



Well, neither are spaces in your regex... yet, it prints it as valid.

The reason is because you are using the find() method, which doesn't match the whole string, it only finds the substrings that matches.


BTW, at this point, you can either stay with your code that uses find() or go back to the code that uses matches(). If you choose the former, you need to have a regex that forces the whole string to be searched.

Henry
[ October 29, 2007: Message edited by: Henry Wong ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find if there is a number in a string using regular expressions.