• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic