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!

2
  • It's duplicate of stackoverflow.com/questions/68633/… Commented Jul 20, 2015 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... Commented Jul 20, 2015 at 10:17

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

But it will matches for all datatypes not only int.
Generally mobile number is in either Integer or String format
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.

2 Comments

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)) {
I cannot judge how the methods are best defined. Calling objectMultiFormat(1234567, numww) should do. Rename as you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.