# Notes
# Regex
Java Subexpression | Matches |
---|---|
| Matches beginning of line. |
| Matches end of line. |
| Matches any single character except newline. Using m option allows it to match newline as well. |
| Matches any single character in brackets. |
| Matches any single character not in brackets. |
| Beginning of entire string |
| End of entire string |
| End of entire string except allowable final line terminator. |
| Matches 0 or more occurrences of preceding expression. |
| Matches 1 or more of the previous thing. |
| Matches 0 or 1 occurrence of preceding expression. |
| Matches exactly n number of occurrences of preceding expression. |
| Matches n or more occurrences of preceding expression. |
| Matches at least n and at most m occurrences of preceding expression. |
| Matches either a or b. |
| Matches word characters. |
| Matches nonword characters. |
| Matches whitespace. Equivalent to |
| Matches nonwhitespace. |
| Matches digits. Equivalent to |
| Matches nondigits. |
| Matches beginning of string. |
| Matches end of string. If a newline exists, it matches just before newline. |
| Matches end of string. |
| Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. |
Referece sites:
http://www.regexr.com
http://www.tutorialspoint.com/java/java_regular_expressions.htm
# demo
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class EmailValidator { | |
// Regex pattern to valid email address | |
private static final String EMAIL_REGEX= | |
"^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-z]{2,})$"; | |
//static Pattern object, since pattern is fixed | |
private static Pattern pattern; | |
//non-static Matcher object because it's created from the input String | |
private Matcher matcher; | |
public EmailValidator(){ | |
//initialize the Pattern object | |
pattern = Pattern.compile(EMAIL_REGEX, Pattern.CASE_INSENSITIVE); | |
} | |
/** | |
* This method validates the input email address with EMAIL_REGEX pattern | |
* @param email | |
* @return boolean | |
*/ | |
public boolean validateEmail(String email){ | |
matcher = pattern.matcher(email); | |
return matcher.matches(); | |
} | |
} | |
public class EmailValidatorTest { | |
// list of valid email addresses | |
private static final String[] validEmailIds = new String[] { | |
"papadev@yahoo.com", | |
"papadev-100@yahoo.com", "papadev.100@yahoo.com", | |
"papadev111@papadev.com", "papadev-100@papadev.net", | |
"papadev.100@papadev.com.au", "papadev@1.com", | |
"papadev@gmail.com.com", "papadev+100@gmail.com", | |
"papadev-100@yahoo-test.com", "papadev_100@yahoo-test.ABC.CoM" }; | |
// list of invalid email addresses | |
private static final String[] invalidEmailIds = new String[] { "papadev", | |
"papadev@.com.my", | |
"papadev123@gmail.a", "papadev123@.com", "papadev123@.com.com", | |
".papadev@papadev.com", "papadev()*@gmail.com", "papadev@%*.com", | |
"papadev..2002@gmail.com", "papadev.@gmail.com", | |
"papadev@papadev@gmail.com", "papadev@gmail.com.1a" }; | |
private static EmailValidator emailValidator; | |
public static void main(String args[]){ | |
emailValidator = new EmailValidator(); | |
for (String temp : validEmailIds) { | |
boolean valid = emailValidator.validateEmail(temp); | |
System.out.println("Email ID "+temp+" is valid? " + valid); | |
} | |
System.out.println("\n\n"); | |
for (String temp : invalidEmailIds) { | |
boolean valid = emailValidator.validateEmail(temp); | |
System.out.println("Email ID "+temp+" is valid? " + valid); | |
} | |
} | |
} |
Email ID papadev@yahoo.com is valid? true
Email ID papadev-100@yahoo.com is valid? true
Email ID papadev.100@yahoo.com is valid? true
Email ID papadev111@papadev.com is valid? true
Email ID papadev-100@papadev.net is valid? true
Email ID papadev.100@papadev.com.au is valid? true
Email ID papadev@1.com is valid? true
Email ID papadev@gmail.com.com is valid? true
Email ID papadev+100@gmail.com is valid? true
Email ID papadev-100@yahoo-test.com is valid? true
Email ID papadev_100@yahoo-test.ABC.CoM is valid? true
Email ID papadev is valid? false
Email ID papadev@.com.my is valid? false
Email ID papadev123@gmail.a is valid? false
Email ID papadev123@.com is valid? false
Email ID papadev123@.com.com is valid? false
Email ID .papadev@papadev.com is valid? false
Email ID papadev()*@gmail.com is valid? false
Email ID papadev@%*.com is valid? false
Email ID papadev..2002@gmail.com is valid? false
Email ID papadev.@gmail.com is valid? false
Email ID papadev@papadev@gmail.com is valid? false
Email ID papadev@gmail.com.1a is valid? false
# Review
Regular Expressions (RegEx) - For text searching & manipulation
Set up a patten to match given tasks for given strings below.
String/phrase | Task | Your Pattern |
---|---|---|
"cat cat cat cattie cat" | Find Cat occurrences | "\\bcat\\b" note use of the added ( \ ) character used as an escape sequence |
doggy | Find dog | dog |
Apple123 | Must start with Apple | [Apple].* |
Apple123 | Must NOT start with Apple | [^Apple].* |
Apple123 | Must start with Apple or apple | [Apple|apple].* |