Sunday, February 21, 2016

Using REGEX


Summary of Regular Expression Constructs

(Source: Oracle Website)

Characters
width="132"xThe character x
\\The backslash character
\0nThe character with octal value 0n (0 <= n <= 7)
\0nnThe character with octal value 0nn (0 <= n <= 7)
\0mnnThe character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
\xhhThe character with hexadecimal value 0xhh
\uhhhhThe 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)
\tThe tab character (‘\u0009’)
\nThe newline (line feed) character (‘\u000A’)
\rThe carriage-return character (‘\u000D’)
\fThe form-feed character (‘\u000C’)
\aThe alert (bell) character (‘\u0007’)
\eThe escape character (‘\u001B’)
\cxThe 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)
\dA digit: [0-9]
\DA non-digit: [^0-9]
\sA whitespace character: [ \t\n\x0B\f\r]
\SA non-whitespace character: [^\s]
\wA word character: [a-zA-Z_0-9]
\WA non-word character: [^\w]

Boundary matchers
^The beginning of a line
$The end of a line
\bA word boundary
\BA non-word boundary
\AThe beginning of the input
\GThe end of the previous match
\ZThe end of the input but for the final terminator, if any
\zThe 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