import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpressionExample {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Validating Email:"
+ isEmailValid("abc@xysss.comccccc"));
System.out.println("Validating Phone Number:"
+ isPhoneNumberValid("1234567798"));
System.out.println("Validating SSN:"
+ isSSNValid("23456-werfdf"));
System.out.println("Validating Numeric Values:"
+ isNumeric("-1234"));
}
public static boolean isEmailValid(String aEmail) {
boolean isValid = false;
/*
* Examples:The following email addresses will pass validation
* asdf@tttdd.com xyzzz@tyyy.in zxg.asd@tx.govt
*/
String emailExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = aEmail;
Pattern pattern = Pattern.compile(emailExpression,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
/**
* isPhoneNumberValid: Validate phone number using Java reg ex. This method
* checks if the input string is a valid phone number.
*
* @param aPhoneNumber
* String. Phone number to validate
* @return boolean: true if phone number is valid, false otherwise.
*/
public static boolean isPhoneNumberValid(String aPhoneNumber) {
boolean isValid = false;
/*
* Examples: Matches following phone numbers: (123)456-7890,
* 123-456-7890, 1234567890, (123)-456-7890
*
*/
String phoneNumberExpression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
CharSequence inputStr = aPhoneNumber;
Pattern pattern = Pattern.compile(phoneNumberExpression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
/**
* isSSNValid: Validate Social Security number (SSN) using Java reg ex. This
* method checks if the input string is a valid SSN.
*
* @param email
* String. Social Security number to validate
* @return boolean: true if social security number is valid, false
* otherwise.
*/
public static boolean isSSNValid(String aSSN) {
boolean isValid = false;
/*
* SSN format xxx-xx-xxxx, xxxxxxxxx, xxx-xxxxxx; xxxxx-xxxx: Examples:
* 879-89-8989; 869878789 etc.
*/
String ssnExpression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
CharSequence inputStr = aSSN;
Pattern pattern = Pattern.compile(ssnExpression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
/**
* isNumeric: Validate a number using Java regex. This method checks if the
* input string contains all numeric characters.
*
* @param email
* String. Number to validate
* @return boolean: true if the input is all numeric, false otherwise.
*/
public static boolean isNumeric(String aNumber) {
boolean isValid = false;
/*
* Number: A numeric value will have following format:
*
* ^[-+]? : Starts with an optional “+” or “-” sign. [0-9]* : May have
* one or more digits. \\.? : May contain an optional “.” (decimal
* point) character. [0-9]+$ : ends with numeric digit.
*
*/
String numericExpression = "^[-+]?[0-9]*\\.?[0-9]+$";
CharSequence inputStr = aNumber;
Pattern pattern = Pattern.compile(numericExpression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
}
No comments