Summary of Regular Expression Constructs
(Source: Oracle Website)Characters | |
width="132"x | The character x |
\\ | The backslash character |
\0n | The character with octal value 0n (0 <= n <= 7) |
\0nn | The character with octal value 0nn (0 <= n <= 7) |
\0mnn | The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7) |
\xhh | The character with hexadecimal value 0xhh |
\uhhhh | The character with hexadecimal value 0xhhhh |
\x{h…h} | The character with hexadecimal value 0xh…h (Character.MIN_CODE_POINT <= 0xh…h <= Character.MAX_CODE_POINT) |
\t | The tab character (‘\u0009’) |
\n | The newline (line feed) character (‘\u000A’) |
\r | The carriage-return character (‘\u000D’) |
\f | The form-feed character (‘\u000C’) |
\a | The alert (bell) character (‘\u0007’) |
\e | The escape character (‘\u001B’) |
\cx | The control character corresponding to x |
Character classes | |
[abc] | a, b, or c (simple class) |
[^abc] | Any character except a, b, or c (negation) |
[a-zA-Z] | a through z or A through Z, inclusive (range) |
[a-d[m-p]] | a through d, or m through p: [a-dm-p] (union) |
[a-z&&[def]] | d, e, or f (intersection) |
[a-z&&[^bc]] | a through z, except for b and c: [ad-z] (subtraction) |
[a-z&&[^m-p]] | a through z, and not m through p: [a-lq-z](subtraction) |
Predefined character classes | |
. | Any character (may or may not match line terminators) |
\d | A digit: [0-9] |
\D | A non-digit: [^0-9] |
\s | A whitespace character: [ \t\n\x0B\f\r] |
\S | A non-whitespace character: [^\s] |
\w | A word character: [a-zA-Z_0-9] |
\W | A non-word character: [^\w] |
Boundary matchers | |
^ | The beginning of a line |
$ | The end of a line |
\b | A word boundary |
\B | A non-word boundary |
\A | The beginning of the input |
\G | The end of the previous match |
\Z | The end of the input but for the final terminator, if any |
\z | The end of the input |
Example:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatchesSample
{
private static String pattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static Pattern customPattern = Pattern.compile(pattern);
public static void main( String args[] ){
String validEmail1 = "testemail@domain.com";
String validEmail2 = "test.email@domain.com";
String invalidEmail1 = "....@domain.com";
String invalidEmail2 = ".$$%%@domain.com";
System.out.println("Is Email ID : '" + validEmail1+ "' valid? - "+validateEmailID(validEmail1));
System.out.println("Is Email ID : '" + validEmail2+ "' valid? - "+validateEmailID(validEmail2));
System.out.println("Is Email ID : '" + invalidEmail1+ "' valid? - "+validateEmailID(invalidEmail1));
System.out.println("Is Email ID : '" + invalidEmail2+ "' valid? - "+validateEmailID(invalidEmail2));
}
public static boolean validateEmailID(String emailID) {
Matcher mtch = customPattern.matcher(emailID);
if(mtch.matches()){
return true;
}
return false;
}
}
Result:
Is Email ID : 'testemail@domain.com' valid? - true
Is Email ID : 'test.email@domain.com' valid? - true
Is Email ID : '....@domain.com? - false
Is Email ID : '.$$%%@domain.com' valid? - false
No comments:
Post a Comment