• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pattern String to validate that a String contains a number or not  RSS feed

 
Ayan Dutta
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I need to validate a String contains a number or not using regex .
It can
1. an integer (10,20 etc).
2.or float (10.22,100.5698)

i.e the no of digits after and before the decimal place can vary.
I need to check it in a single place.

Can any body the sample java.util.regex Pattern String for it .
It is little bit urgent for me .
Regards,
Ayan
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ayan,
I am not sure if I got your question right or not.
I suppose you want to get a match like the following

90.90
0.90
90

and reject alphanumeric, special characters and invalid numeric entries like .90, 90. , 90.90.90 , 90..90 etc, then the following is the code. Please validate it.

public static void main(String[] args) {

String regex = "(\\d+)((\\.{1}\\d+)?)";
String test = "90.90";

if(test.matches(regex))
{
System.out.println("matches");
}
else
{
System.out.println("not matched");
}
}


Hope it was helpful!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!