The blog of Jafar Aliyev


Creation date:

Pattern Matching

First look at the normal code with + greedy quantifier. In this example we try to find sequences containing at least one a character. Hence our pattern is a+

  Pattern p = Pattern.compile("a+");
  Matcher m = p.matcher("ab4 5aaaa6_7ab");
  System.out.println("Pattern is " + m.pattern());
  while(m.find()) {
    System.out.println(m.start() + " " +;

We'll get quite expectable output

 Pattern is a+
 0 a
 5 aaaa
 12 a

+ sign is a greedy quantifier, therefore we got all a symbols starting from 5th position in one group. Now let's change + to * greedy quantifier in our pattern and execute the same code with a* pattern.

 0 a
 5 aaaa
 12 a

As you see, we got a strange result. Do you have any idea about this result? I'll try to explain. Because of the nature of * quantifier, matcher looks for zero or more occurrences of the a character. That's why it gives every position in result, even after last character.

We'll have the similar result with ? quantifier. Only the line 5 will be spanned to 4 lines with a single a in each line.

Author: Jafar N.Aliyev (Jsoft)

Read also

Use instanceof carefully
Usage of 'instanceof' method in some situations will give you a compilation error

Wrapper objects

Static and instance initilization blocks


Java access and nonaccess modifiers
Look at this cheat sheet for Java access and nonaccess modifiers

Implicit typecasting

Protected details

Interface methods are always public

Static methods can not be overriden
Here I explain, why static methods can not be overriden

Implicit typecasting

Java Tips

Variable hiding

Postfix operators

Tricky Polymorphism

Default values

String equality

© Copyright

All articles in this site are written by Jafar N.Aliyev. Reproducing of any article must be followed by the author name and link to the site of origin(this site). This site also keeps the same rules relative to the articles of other authors.