0

Hi am new to using regular expression as generic methods in java.I am confusing with How to use Regular Expression as generic methods in Java. Generally Regular expression will take String type but i need a requirement for it will accept all datatypes like int, double, float..... Below is my code it returns true or false value based on regular expression and Given data. But my question is here it valid only for String values.I need it on Int format(like Generic methods).

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

public class StringValidator {
public boolean stringMultiFormat(String str, String regex) {
    boolean isValid = false;
    CharSequence inputStr = str;
    // Make the comparison case-insensitive.
    Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);

    if (matcher.matches()) {
        isValid = true;
        System.out.println(isValid);
    } else {
        System.out.println(isValid);
    }
    return isValid;
}

public static void main(String[] args) {
    StringValidator aiov = new StringValidator();

    String num = "^\\s*(?:\\+?(\\d{1,3}))?([-. (]*(\\d{3})[-. )]*)?((\\d{3})[-. ]*(\\d{2,4})(?:[-.x ]*(\\d+))?)\\s*$";

    if (aiov.stringMultiFormat("(650)-055-2345", num)) {
        System.out.println("Given number is Valid Mobile Number");
    } else {
        System.out.println("Given number is Invalid Mobile Number");
    }

}
}

Thanks in Advance!

milez
  • 2,201
  • 12
  • 31
Sai
  • 311
  • 1
  • 3
  • 18
  • It's duplicate of http://stackoverflow.com/questions/68633/regex-that-will-match-a-java-method-declaration – Anilkumar Bathula Jul 20 '15 at 10:12
  • 3
    BTW, I hope you are not considering using a number type for storing phone numbers! Think of, e.g., phone numbers with leading zeros... – tobias_k Jul 20 '15 at 10:17

3 Answers3

1

There are two ways you can make stringMultiFormat accept multiple formats. The first is to change your input parameter from String to Object. In Java all classes inherit from Object, which means Object parameters can accept many class inputs. You can then use casting, for example

public boolean stringMultiFormat(Object var, String regex) {
    if (var instanceof String)
    { String str = (String) var; }
}

Alternatively you can use method overloading.

milez
  • 2,201
  • 12
  • 31
0

If you want to check if an integer matches a given regular expression, you can just convert it to a String:

int test = 12345;
String testAsString = String.valueOf(test);
if (aiov.stringMultiFormat(testAsString, regex)) {

}

By the way, this has nothing to do with Generics.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0

In theory you could use an Object or generic type as follows:

public boolean objectMultiFormat(Object value, String regex) {
    return value == null ? false 
         : aiov.stringMultiFormat(String.valueOf(value), regex));
}

Above because of the null case even value.toString() would do.

however Integer, Doubleand such have a stricter pattern. Also the valueOf is locale independent: no thousands separator, no decimal comma. This probably implies that the original entry before getting an int had its own validation, Format.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • following your steps am getting an error like `Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence` from bellow snippet `if (aiov.stringMultiFormat(1234567, numww)) {` – Sai Jul 20 '15 at 10:28
  • I cannot judge how the methods are best defined. Calling `objectMultiFormat(1234567, numww)` should do. Rename as you want. – Joop Eggen Jul 20 '15 at 10:49