Tuesday, August 26, 2008

Java Code: Using Reg Ex to identify Strings rep of int/float


Using Reg Ex to identify String representation of int/float (Whitespaces allowed)

Find below a complete Java program illustrating the usage of Regular Expressions (introduced in Java 1.4) to identify String representations of int or float values. The program first tries to identify Strings representing only a valid int value without any leading, trailing or embedded whitespaces and subsequently moves on to identify all possible String representations of int/float values with whitespaces embedded anywhere in it. Download the code as a PDF doc if the below listing looks very clumsy. Unfortunately I'm yet to find a better way to display source code in Blogger.


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExStringToNumTest {
public static void main(String[] args) {

//... basic int String having no Whitespaces
String intString = "-100"; //... positive test case
String intString_ntc = "-"; //... negative test case

Pattern patternForIntWithoutWSH = Pattern.compile( "((-?+)|(\\+{0,1}+))([0-9]+)" );

Matcher mi = patternForIntWithoutWSH.matcher(intString);
System.out.println("RegEx for int without Whitespaces - ((-?+)|(\\+{0,1}+))([0-9]+)");
System.out.println("Positive Test Case - For String \"" + intString + "\" return value: " + mi.matches());

Matcher mi_ntc = patternForIntWithoutWSH.matcher(intString_ntc);
System.out.println("Negative Test Case - For String \"" + intString_ntc + "\" return value: " + mi_ntc.matches());

//... int String with Leading or Trailing Whitespaces
String iswlotw = " - 1007 "; //... positive test case
String iswlotw_ntc = " - "; //... negative test case

Pattern patternForIntWithWSH = Pattern.compile( "([ ]*)((-?+)|(\\+{0,1}+))([ ]*)([0-9]+)([ ]*)" );

Matcher mi1 = patternForIntWithWSH.matcher(iswlotw);
System.out.println("\nRegEx for int with Leading/Trailing Whitespaces - ([ ]*)((-?+)|(\\+{0,1}+))([ ]*)([0-9]+)([ ]*)");
System.out.println("Positive Test Case - For String \"" + iswlotw + "\" return value: " + mi1.matches());

Matcher mi1_ntc = patternForIntWithWSH.matcher(iswlotw_ntc);
System.out.println("Negative Test Case - For String \"" + iswlotw_ntc + "\" return value: " + mi1_ntc.matches());

//... int String with complete Whitespace Handling - Embedded and/or Leading/Trailing
String iscomplete = " - 2 0 " +
"0 1 " +
" 009"; //... positive test case
String iscomplete_ntc = " - "; //... negative test case
Pattern patternForIntWithCompleteWSH = Pattern.compile( "([ ]*)((-?+)|(\\+{0,1}+))([ ]*)([0-9]+)([ 0-9 ]*)([ ]*)" );

Matcher mi2 = patternForIntWithCompleteWSH.matcher(iscomplete);
System.out.println("\nRegEx for complete int processing - ([ ]*)((-?+)|(\\+{0,1}+))([ ]*)([0-9]+)([ 0-9 ]*)([ ]*)");
System.out.println("Positive Test Case - for String \"" + iscomplete + "\" returns: " + mi2.matches());

Matcher mi2_ntc = patternForIntWithCompleteWSH.matcher(iscomplete_ntc);
System.out.println("Negative Test Case - For String \"" + iscomplete_ntc + "\" return value: " + mi2_ntc.matches());

//... float String with complete Whitespace Handling
String floatString = " -1 007 . 0 5 "; //... positive test case
String floatString_ntc1 = " - 9 . "; //... negative test case - 1
String floatString_ntc2 = " - . 8 "; //... negative test case - 2
String floatString_ntc3 = " - . "; //... negative test case - 3

Pattern patternForFloat = Pattern.compile( "([ ]*)((-?+)|(\\+{0,1}+))([ ]*)([0-9]+)([ 0-9 ]*)([ ]*)((\\.)([ ]*[0-9]+))?+([ ]*)([ 0-9 ]*)([ ]*)" );

Matcher mf = patternForFloat.matcher(floatString);
System.out.println("\nRegEx for complete float processing - ([ ]*)((-?+)|(\\+{0,1}+))([ ]*)([0-9]+)([ 0-9 ]*)([ ]*)((\\.)([0-9]+))?+([ ]*)([ 0-9 ]*)([ ]*)");
System.out.println("Positive Test Case - For String \"" + floatString + "\" returns: " + mf.matches());

Matcher mf_ntc1 = patternForFloat.matcher(floatString_ntc1);
System.out.println("Negative Test Case #1 - For String \"" + floatString_ntc1 + "\" return value: " + mf_ntc1.matches());

Matcher mf_ntc2 = patternForFloat.matcher(floatString_ntc2);
System.out.println("Negative Test Case #2 - For String \"" + floatString_ntc2 + "\" returns: " + mf_ntc2.matches());

Matcher mf_ntc3 = patternForFloat.matcher(floatString_ntc3);
System.out.println("Negative Test Case #3 - For String \"" + floatString_ntc3 + "\" returns: " + mf_ntc3.matches());
}
}





Output:-

RegEx for int without Whitespaces - ((-?+)|(\+{0,1}+))([0-9]+)
Positive Test Case - For String "-100" return value: true
Negative Test Case - For String "-" return value: false

RegEx for int with Leading/Trailing Whitespaces - ([ ]*)((-?+)|(\+{0,1}+))([ ]*)([0-9]+)([ ]*)
Positive Test Case - For String " - 1007 " return value: true
Negative Test Case - For String " - " return value: false

RegEx for complete int processing - ([ ]*)((-?+)|(\+{0,1}+))([ ]*)([0-9]+)([ 0-9 ]*)([ ]*)
Positive Test Case - for String " - 2 0 0 1 009" returns: true
Negative Test Case - For String " - " return value: false

RegEx for complete float processing - ([ ]*)((-?+)|(\+{0,1}+))([ ]*)([0-9]+)([ 0-9 ]*)([ ]*)((\.)([0-9]+))?+([ ]*)([ 0-9 ]*)([ ]*)
Positive Test Case - For String " -1 007 . 0 5 " returns: true
Negative Test Case #1 - For String " - 9 . " return value: false
Negative Test Case #2 - For String " - . 8 " returns: false
Negative Test Case #3 - For String " - . " returns: false



If you are not quite familiar with Regular Expressions and want a step by step explanation of this program then please read this article - Using Reg Ex to identify Strings containing int/float >>



Share/Save/Bookmark


2 comments:

Anonymous said...

Quite extensive and thorough. Interesting article. It covers almost all possible combinations. Thanks!

Geek said...

Good to know that you liked the article. Thanks for your visit. Keep visiting/posting.