﻿ Javac · Java Tutorials - Regex Quantifier Introduction Tutorial

### Regex Quantifier Introduction Tutorial

Quantifiers utilize the following metacharacters: ?*+{,}
There may be occasions where you want to specify how many times a pattern occurs in a string. Quantifiers allow you to do just that - just in ways that you might not expect. There are three basic categories of quantifiers: greedy, reluctant, and possessive. I am going to detail each of the categories in their own separate tutorials, but this tutorial will introduce you to some simple greedy quantifiers. I will demonstrate how to use the {,} metacharacters in this introduction.
There are three possible ways to use those metacharacters as quantifiers:
expression{3} = means the expression must occur EXACTLY three times in the search string.
expression{3,7} = means the expression must occur BETWEEN three to seven times (inclusive) in the search string.
expression{4,} = means the expression must occur AT LEAST four times in the search string.

```Pattern         Matcher                                         .find() result
lime{1}         "5 limes for \$1"                                true
round{2}        "The wheels on the bus go round and round"      false
round{2}        "The wheels on the bus go roundd and round"     true
(round){2}      "The wheels on the bus go round and round"      false
(round){2}      "The wheels on the bus go roundround"           true
```

The following code will build on all concepts discussed in all of my regex tutorials thus far.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

```C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md RegexQuantifierIntro
C:\Java>cd RegexQuantifierIntro
```

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

``````
import java.util.regex.*;

class RegexQuantifierIntro {
public static void main(String args[]) {
displayFind("lime{1}","5 limes for \$1");
displayFind("round{2}","The wheels on the bus go round and round");
displayFind("round{2}","The wheels on the bus go roundd and round");
displayFind("round{2}","The wheels on the bus go roundd and roundd");
displayFind("(round){2}","The wheels on the bus go round and round");
displayFind("(round){2}","The wheels on the bus go roundround");

displayFind("[ae]{4,7}","Hello everybody, I\'m Dan");
displayFind("[ae]{4,7}","eeeeeee");
displayFind("[ae]{4,7}","eaee");
displayFind("[ae]{4,}","oh craaaaaaap!");

int i = 0;
while(!Pattern.compile("20{4}").matcher(""+ ++i).find()) { }
System.out.println(i + " leagues under the sea.\n");

displayFind("\\d{3}-\\d{4}","867-5309");
displayFind("\\d{3}-\\d{4}","5309-867");
displayFind("\\d{3}-\\d{4}","1867-5309");
displayFind("[ae]","Bear");
displayFind("[^ae]","Bear");
displayFind("^\\d{3}-\\d{4}","1867-5309");
displayFind("^\\d{3}-\\d{4}","867-5309");
displayFind("\\d{3}-\\d{4}","867-53099");
displayFind("\\d{3}-\\d{4}\$","867-53099");
displayFind("^\\d{3}-\\d{4}\$","867-5309");
}

static void displayFind(String regex, String searchMe) {
boolean foundIt = false;
Matcher m = Pattern.compile(regex).matcher(searchMe);
while(m.find()){
System.out.println("Found: " + m.group() + " at index: "+ m.start() + " for regex " + regex + " in string \"" + searchMe +"\"" );
foundIt = true;
}
if(!foundIt){
System.out.println("No matches found for " + regex + " in string \"" + searchMe +"\'");
}
System.out.println();
}
}
``````

Now switch back to the command prompt (CMD) and type in javac RegexQuantifierIntro.java and press Enter.
Now type in java RegexQuantifierIntro and press Enter.

```
C:\Java\RegexQuantifierIntro>javac RegexQuantifierIntro.java
C:\Java\RegexQuantifierIntro>java RegexQuantifierIntro
Found: lime at index: 2 for regex lime{1} in string "5 limes for \$1"

No matches found for round{2} in string "The wheels on the bus go round and round'

Found: roundd at index: 25 for regex round{2} in string "The wheels on the bus go roundd and round"

Found: roundd at index: 25 for regex round{2} in string "The wheels on the bus go roundd and roundd"
Found: roundd at index: 36 for regex round{2} in string "The wheels on the bus go roundd and roundd"

No matches found for (round){2} in string "The wheels on the bus go round and round'

Found: roundround at index: 25 for regex (round){2} in string "The wheels on the bus go roundround"

No matches found for [ae]{4,7} in string "Hello everybody, I'm Dan'

Found: eeeeeee at index: 0 for regex [ae]{4,7} in string "eeeeeee"

Found: eaee at index: 0 for regex [ae]{4,7} in string "eaee"

Found: aaaaaaa at index: 5 for regex [ae]{4,} in string "oh craaaaaaap!"

Found: 867-5309 at index: 0 for regex \d{3}-\d{4} in string "867-5309"

No matches found for \d{3}-\d{4} in string "5309-867'

Found: 867-5309 at index: 1 for regex \d{3}-\d{4} in string "1867-5309"

Found: e at index: 1 for regex [ae] in string "Bear"
Found: a at index: 2 for regex [ae] in string "Bear"

Found: B at index: 0 for regex [^ae] in string "Bear"
Found: r at index: 3 for regex [^ae] in string "Bear"

No matches found for ^\d{3}-\d{4} in string "1867-5309'

Found: 867-5309 at index: 0 for regex ^\d{3}-\d{4} in string "867-5309"

Found: 867-5309 at index: 0 for regex \d{3}-\d{4} in string "867-53099"

No matches found for \d{3}-\d{4}\$ in string "867-53099'

Found: 867-5309 at index: 0 for regex ^\d{3}-\d{4}\$ in string "867-5309"

```

### Final thoughts

Quantifiers may not work how you initially expect them to. Now that you understand some basics about how they function I am going to do some tutorials that spotlight some of the methods of both the Pattern and the Matcher classes.