File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pramod Kumar
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pramod Kumar
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
bart zagers
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pramod Kumar
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic