# Notes

# Regex

Java SubexpressionMatches

^

Matches beginning of line.
匹配行首。

$

Matches end of line.
匹配行尾。

.

Matches any single character except newline. Using m option allows it to match newline as well.
匹配除换行符以外的任何单个字符。使用 m 选项还可以使其与换行符匹配。

[...]

Matches any single character in brackets.
匹配括号中的任何单个字符。

[^...]

Matches any single character not in brackets.
匹配括号中没有的任何单个字符。

\A

Beginning of entire string
整个字符串的开头

\z

End of entire string
整个字符串的结尾

\Z

End of entire string except allowable final line terminator.
整个字符串的结尾,但允许的最后一行终止符除外。

re*

Matches 0 or more occurrences of preceding expression.
匹配 0 个或多个出现的前一个表达式。

re+

Matches 1 or more of the previous thing.
匹配上一项或多项。

re?

Matches 0 or 1 occurrence of preceding expression.
匹配 0 或 1 个出现的前一个表达式。

re{ n}

Matches exactly n number of occurrences of preceding expression.
精确匹配前一个表达式的 n 次出现。

re{ n,}

Matches n or more occurrences of preceding expression.
匹配 n 个或多个出现的前一个表达式。

re{ n, m}

Matches at least n and at most m occurrences of preceding expression.
至少匹配 n 个并且最多匹配 m 个先前的表达式。

a|b

Matches either a or b.
匹配 a 或 b。

\w

Matches word characters.
匹配单词字符。

\W

Matches nonword characters.
匹配非单词字符。

\s

Matches whitespace. Equivalent to [\t\n\r\f] .
匹配空格。等效于 [\t\n\r\f]

\S

Matches nonwhitespace.
匹配非空格。

\d

Matches digits. Equivalent to [0-9] .
匹配数字。相当于 [0-9]

\D

Matches nondigits.
匹配非数字。

\A

Matches beginning of string.
匹配字符串的开头。

\Z

Matches end of string. If a newline exists, it matches just before newline.
匹配字符串的结尾。如果存在换行符,则匹配换行符。

\z

Matches end of string.
匹配字符串的结尾。

\b

Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.
放在方括号内时匹配单词边界。放在方括号内时,匹配退格键(0x08)。

Referece sites:
http://www.regexr.com
http://www.tutorialspoint.com/java/java_regular_expressions.htm

# demo

EmailValidator.java
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/phraseTaskYour Pattern
"cat cat cat cattie cat"Find Cat occurrences"\\bcat\\b" note use of the added ( \ ) character used as an escape sequence
doggyFind dogdog
Apple123Must start with Apple[Apple].*
Apple123Must NOT start with Apple[^Apple].*
Apple123Must start with Apple or apple[Apple|apple].*