Listing 1
Anchors:
\b, \B word boundary
^, \A Start of string/line
$, \z, \Z End of string/line
(?=..) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind
(?<!....) Negative lookbehind
Character Classes:
[...] include a list of characters
[^...] exclude a list of characters
Class Shorthands:
\d digit, Shorthand for [0-9]
\D non-digit, Shorthand for [^0-9]
\s whitespace character, Shorthand for [.\n\t\f\r\x0B]
\S non-whitespace, Shorthand for [^\s]
\w word character, Shorthand for [a-zA-Z0-9_]
\W not-word character, Shorthand for [^a-zA-Z0-9_] or [^\w]
Quantifiers:
Greedy:
? 0 or 1 instances
* 0 or more instances
+ 1 or more instances
{n} exactly n instances
{n,} n or more instances
{n,m} between n and m instances
Reluctant: ??, *?, +?, {n}?, {n,}?, {n,m}?
Possessive: ?+, *+, ++, {n}+, {n,}+, {n,m}+
Conditionals:
| indicates alternation
Mode modifiers:
i Case insensitive mode (Pattern.CASE_INSENSITIVE)
m Multi-line mode (Pattern.MULTILINE)
s Dot match all mode (Pattern.DOTALL)
x Free spacing and comment mode (Pattern.COMMENTS)
Grouping, Capturing
(?: ...) Grouping only parenthesis
(...) \1 \2 \3 ... Capturing parenthesis
Listing 2
1 public boolean validate(String password)
2 {
3 return password.matches("([a-zA-Z]*\\d+[a-zA-Z]*)") &&
4 password.matches("([a-zA-Z0-9]){6,32}");
5 }
Listing 3
1 import java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3 ...
4 ...
5 Pattern pContent = Pattern.compile("\\b # Indicate the pattern boundary \n"
6 +"(?: # Grouping only parenthesis for clarity and speed \n"
7 +"(?i) # Change the match mode to case insensitive \n"
8 +"[a-z] # The character class matches any alphabet character \n"
9 +"* # The * quantifier indicates presence of 0 or more alphabets \n"
10 +"\\d # Match any digits between [0-9] \n"
11 +"+ # Quantifier + indicates presence of 1 or more digits \n"
12 +"[a-z]* #Followed by any number of characters \n"
13 +") #End group \n"
14 +"\\b #word boundary \n"
15 , Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
16
17 Pattern pLength = Pattern.compile("\\b # Indicate the pattern boundary \n"
18 +"(?: #Grouping only parenthesis for clarity and speed\n"
19 +"[a-z0-9] # The character class matches any alphanumeric character \n"
20 +") #End group \n "
21 +"{6,32} #{min,max} quantifier to indicate the match limits\n "
22 +"\\b #word boundary \n"
23 , Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
24
25 private boolean validates(String password)
26 {
27 return
28 pContent.matcher(password).matches() &&
29 pLength.matcher(password).matches();
30 }
Listing 4
1 import java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3 ...
4 ...
5 Pattern p = Pattern.compile("(\\d)");
6 Matcher m = p.matcher(str);
7 private String replaceDigits(String str)
8 {
9 StringBuffer newPassword = new StringBuffer();
10 int iArray[] = {9,8,7,6,5,4,3,2,1,0};
11
12 while (m.find())
13 {
14 int i = Integer.parseInt(m.group(1));
15 i = iArray[i];
16 m.appendReplacement(newPassword,String.valueOf(i));
17 }
18 m.appendTail(newPassword);
19 return newPassword.toString();
20 }
Listing 5
1 Pattern pEmail =
2 Pattern.compile("^
# Indicate the begin anchor \n" +
3 "From:" +
4 "(\\w[-.\\w]*.*@matrix\\.(?:com|net|org)) #Email address match \n" +
5 "\\s* # Followed by optional whitespaces \n" + "\\(([^()]*)\\)
# Followed by the person name \n"
7 , Pattern.CASE_INSENSITIVE|Pattern.COMMENTS);
8
9 Pattern pURL = Pattern.compile(
10 "\\b # Indicate the pattern boundary \n" +
11 "(http://" +
12 "\\w+(?:[\\.\\w+])*\\.(?:com|net|org|edu) # Hostname match \n" +
13 "(?:/[-\\w:~!\\@\\$%&*?=+,./']*)? # Followed by an optional path \n" +
14 "\\b # Pattern boundary \n" +
15 ")",
16 Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.COMMENTS);