Match a particular character a specified number of times. : Introduction « Regular Expressions « Java Tutorial






There are two general ways the repetition operators work.

One class of operators is greedy, that is, they match as much as they can, until the end.

The other class is reluctant (or lazy), and matches only to the first chance they can terminate.

For example, the regular expression .*; matches any number of characters up to the last semicolon it finds.

To only match up to the first semicolon, the reluctant version .*?; must be used.

Greedy OperatorDescription
X?Matches X zero or one time
X*Matches X zero or more times
X+Matches X one or more times
X{n}Matches X exactly n times, where n is any number
X{n,}Matches X at least n times
X{n,m}Matches X at least n, but no more than m times


Reluctant (Lazy) OperatorDescription
X??Matches X zero or one time
X*?Matches X zero or more times
X+?Matches X one or more times
X{n}?Matches X exactly n times, where n is any number
X{n,}?Matches X at least n times
X{n,m}?Matches X at least n, but no more than m times










8.1.Introduction
8.1.1.Meta-characters predefined to match specific characters.
8.1.2.Meta-characters to match against certain string boundaries.
8.1.3.Regular expression languages also have character classes.
8.1.4.POSIX character classes and Java character classes
8.1.5.Java Character Class
8.1.6.Match a particular character a specified number of times.
8.1.7.Read regular expression from console
8.1.8.Regex Test Harness
8.1.9.Match Java source file and file and class name
8.1.10.Finding all words that start with an 'a'
8.1.11.Simple validation using the Pattern and Matcher objects
8.1.12.A possessive qualifier
8.1.13.Find the starting point of the second 'Bond'
8.1.14.A negative look ahead
8.1.15.A negative behind ahead
8.1.16.A positive look ahead
8.1.17.Pattern helper
8.1.18.Escapes characters that have special meaning to regular expressions