Prerequisites
Development environment
- JDK 1.8 Downloads
- Eclipse Mars Download
Quantifiers allow you to specify the number of occurrences to match against. For convenience, the three sections of the Pattern API specification describing greedy, reluctant, and possessive quantifiers are presented below:
Greedy | Reluctant | Possessive | Meaning |
X? | X?? | X?+ | X, once or not at all |
X* | X*? | X*+ | X, zero or more times |
X+ | X+? | X++ | X, one or more times |
X{n} | X{n}? | X{n}+ | X, exactly n times |
X{n,} | X{n,}? | X{n,}+ | X, at least n times |
X{n,m} | X{n,m}? | X{n,m}+ | X, at least n but not more than m times |
Step 1 Create Java Project
Launch Eclipse IDE; Create new Java Project by going to File -> New -> Others… -> Java Project. Choose any project name you want then click Finish.
Step 2 Create Java Class
Copy the code below to clipboard; Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package info.java.tips.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexDemo { public static void main(String[] args) { lookup("", "a?", "a*", "a+"); lookup("aaaaa", "a?", "a*", "a+"); lookup("ababaaaab", "a?", "a*", "a+"); lookup("aaaaaaaaa", "a{3}", "a{3,}", "a{3,6}"); lookup("dogdogdogdogdogdog", "(dog){3}", "dog{3}"); lookup("abccabaaaccbbbc", "[abc]{3}", "abc{3}"); /** * .*foo greedy quantifier * .*?foo reluctant quantifier * .*+foo possessive quantifier */ lookup("xfooxxxxxxfoo", ".*foo", ".*?foo", ".*+foo"); } private static void lookup(String input, String... regexes) { System.out.println("\n\r******************* " + " INPUT '" + input + "' **********************"); for (String regex : regexes) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); boolean found = false; while (matcher.find()) { System.out.println(String.format("REGEX \"%s\": The text \"%s\" starts at " + "index %d and ends at index %d.", regex, matcher.group(), matcher.start(), matcher.end())); found = true; } if (!found) { System.out.println("REGEX \"" + regex + "\": The text \"" + input + "\" doesn't match."); } } } } |
Step 3 Run The Application
Right click to the class; select Run As -> Java Application.
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
******************* INPUT '' ********************** REGEX "a?": The text "" starts at index 0 and ends at index 0. REGEX "a*": The text "" starts at index 0 and ends at index 0. REGEX "a+": The text "" doesn't match. ******************* INPUT 'aaaaa' ********************** REGEX "a?": The text "a" starts at index 0 and ends at index 1. REGEX "a?": The text "a" starts at index 1 and ends at index 2. REGEX "a?": The text "a" starts at index 2 and ends at index 3. REGEX "a?": The text "a" starts at index 3 and ends at index 4. REGEX "a?": The text "a" starts at index 4 and ends at index 5. REGEX "a?": The text "" starts at index 5 and ends at index 5. REGEX "a*": The text "aaaaa" starts at index 0 and ends at index 5. REGEX "a*": The text "" starts at index 5 and ends at index 5. REGEX "a+": The text "aaaaa" starts at index 0 and ends at index 5. ******************* INPUT 'ababaaaab' ********************** REGEX "a?": The text "a" starts at index 0 and ends at index 1. REGEX "a?": The text "" starts at index 1 and ends at index 1. REGEX "a?": The text "a" starts at index 2 and ends at index 3. REGEX "a?": The text "" starts at index 3 and ends at index 3. REGEX "a?": The text "a" starts at index 4 and ends at index 5. REGEX "a?": The text "a" starts at index 5 and ends at index 6. REGEX "a?": The text "a" starts at index 6 and ends at index 7. REGEX "a?": The text "a" starts at index 7 and ends at index 8. REGEX "a?": The text "" starts at index 8 and ends at index 8. REGEX "a?": The text "" starts at index 9 and ends at index 9. REGEX "a*": The text "a" starts at index 0 and ends at index 1. REGEX "a*": The text "" starts at index 1 and ends at index 1. REGEX "a*": The text "a" starts at index 2 and ends at index 3. REGEX "a*": The text "" starts at index 3 and ends at index 3. REGEX "a*": The text "aaaa" starts at index 4 and ends at index 8. REGEX "a*": The text "" starts at index 8 and ends at index 8. REGEX "a*": The text "" starts at index 9 and ends at index 9. REGEX "a+": The text "a" starts at index 0 and ends at index 1. REGEX "a+": The text "a" starts at index 2 and ends at index 3. REGEX "a+": The text "aaaa" starts at index 4 and ends at index 8. ******************* INPUT 'aaaaaaaaa' ********************** REGEX "a{3}": The text "aaa" starts at index 0 and ends at index 3. REGEX "a{3}": The text "aaa" starts at index 3 and ends at index 6. REGEX "a{3}": The text "aaa" starts at index 6 and ends at index 9. REGEX "a{3,}": The text "aaaaaaaaa" starts at index 0 and ends at index 9. REGEX "a{3,6}": The text "aaaaaa" starts at index 0 and ends at index 6. REGEX "a{3,6}": The text "aaa" starts at index 6 and ends at index 9. ******************* INPUT 'dogdogdogdogdogdog' ********************** REGEX "(dog){3}": The text "dogdogdog" starts at index 0 and ends at index 9. REGEX "(dog){3}": The text "dogdogdog" starts at index 9 and ends at index 18. REGEX "dog{3}": The text "dogdogdogdogdogdog" doesn't match. ******************* INPUT 'abccabaaaccbbbc' ********************** REGEX "[abc]{3}": The text "abc" starts at index 0 and ends at index 3. REGEX "[abc]{3}": The text "cab" starts at index 3 and ends at index 6. REGEX "[abc]{3}": The text "aaa" starts at index 6 and ends at index 9. REGEX "[abc]{3}": The text "ccb" starts at index 9 and ends at index 12. REGEX "[abc]{3}": The text "bbc" starts at index 12 and ends at index 15. REGEX "abc{3}": The text "abccabaaaccbbbc" doesn't match. ******************* INPUT 'xfooxxxxxxfoo' ********************** REGEX ".*foo": The text "xfooxxxxxxfoo" starts at index 0 and ends at index 13. REGEX ".*?foo": The text "xfoo" starts at index 0 and ends at index 4. REGEX ".*?foo": The text "xxxxxxfoo" starts at index 4 and ends at index 13. REGEX ".*+foo": The text "xfooxxxxxxfoo" doesn't match. |