Regular Expression
to check numeric String
In order to build a regular expression to check if String is number or
not or if String contains any non digit
character or not you need to learn about character set in Java regular
expression, Which we are going to see in this Java regular expression example.
No doubt Regular Expression is great
tool in developer arsenal and familiarity or some expertise with regular
expression can help you a lot. Java supports regular expression using java.util.regex.Pattern and java.util.regex.Matchter class, you
can see a dedicated package java.util.regex for
regular expression in Java. Java supports regex from JDK 1.4, means well before
Generics, Enum or Autoboxing. If you are writing server side code in Java
programming language than you may be familiar with importance of regular
expression which is key in parsing certain kind of messages e.g. FIX Messages used in
Electronic trading. In order to parse Repeating groups in FIX protocol
you really needs understanding of regular expression in Java. Any way In order
to learn validating numbers using regular expression we will start with simple
example
1) Check
if a String is a number or not using regular expression
to clarify requirement, an String would be number if it contains digits.
we have omitted decimal point and sign or + or
- for simplicity.
If you are familiar with predefined character class in Java regular
expression that you must know that \d will
represent a digit (0-9) and \D will
represent a non digit (anything other than 0 to 9). Now using this
predefined character class, an String will not be a number if
it contains any non digit characters, which can be written in Java regular
expression as:
Pattern pattern = Pattern.compile(".*\\D.*");
which checks for non digit character anywhere in the String. This pattern return
true if String contains any thing other than 0-9
digit, which can be used to know if an String is number
or not using regular expression.
Same regular expression for checking
String for numbers can also be written without using predefined character
set and using character class and negation as shown in following example :
Pattern pattern = Pattern.compile(".*[^0-9].*");
This is similar to above regex pattern, only difference is \D is
replaced by [^0-9]. By the way there is always
multiple ways to check for certain things using regex.
2. Verify
if an String is a six digit number or not using regular expression
This is kind of special regular expression requirement for validating
data like id, zipcode or any
other pure numerical data. In order to
check for digit you can either use character class [0-9]
or use short form \d. here is simple regular expression in Java
which can check if an String contains 6 digits or not:
Pattern digitPattern =
Pattern.compile("\\d\\d\\d\\d\\d\\d");
above pattern checks each character for digit six times. This pattern can
also be written in much shorter and readable format as :
Pattern digitPattern =
Pattern.compile("\\d{6}");
where {6} denote six times. you can also replace \d with character class
[0-9] and it should work.
Code Example - Regular expression in Java to check numbers

import java.util.regex.Pattern;
/**
* Java program to demonstrate use of Regular Expression to check
/**
* Java program to demonstrate use of Regular Expression to check
* if a String is a 6 digit
number or not.
*/
public class RegularExpressionExample {
public static void main(String args[]) {
// Regular expression in Java to check if String is number or not
Pattern pattern = Pattern.compile(".*[^0-9].*");
//Pattern pattern = Pattern.compile(".*\\D.*");
String [] inputs = {"123", "-123" , "123.12", "abcd123"};
for(String input: inputs){
System.out.println( "does " + input + " is number : "
*/
public class RegularExpressionExample {
public static void main(String args[]) {
// Regular expression in Java to check if String is number or not
Pattern pattern = Pattern.compile(".*[^0-9].*");
//Pattern pattern = Pattern.compile(".*\\D.*");
String [] inputs = {"123", "-123" , "123.12", "abcd123"};
for(String input: inputs){
System.out.println( "does " + input + " is number : "
+ !pattern.matcher(input).matches());
}
// Regular expression in java to check if String is 6 digit number or not
String [] numbers = {"123", "1234" , "123.12", "abcd123", "123456"};
Pattern digitPattern = Pattern.compile("\\d{6}");
}
// Regular expression in java to check if String is 6 digit number or not
String [] numbers = {"123", "1234" , "123.12", "abcd123", "123456"};
Pattern digitPattern = Pattern.compile("\\d{6}");
//Pattern digitPattern =
Pattern.compile("\\d\\d\\d\\d\\d\\d");
for(String number: numbers){
System.out.println( "does " + number + " is 6 digit number : "
+ digitPattern.matcher(number).matches());
}
}
}
Output:
does 123 is number : true
does -123 is number : false
does 123.12 is number : false
does abcd123 is number : false
does 123 is 6 digit number : false
does 1234 is 6 digit number : false
does 123.12 is 6 digit number : false
does abcd123 is 6 digit number : false
does 123456 is 6 digit number : true
}
}
}
Output:
does 123 is number : true
does -123 is number : false
does 123.12 is number : false
does abcd123 is number : false
does 123 is 6 digit number : false
does 1234 is 6 digit number : false
does 123.12 is 6 digit number : false
does abcd123 is 6 digit number : false
does 123456 is 6 digit number : true
That's all on using Java regular expression to check numbers in String.
As you have seen in this Java Regular Expression example that its pretty easy
and fun to do validation using regular expression.
Other Java tutorials you may find useful
No comments:
Post a Comment