Regular Expressions and the void in a string (zero-length matches)

While attempting to do a substitution of word characters   “[a-zA-Z0-9_]” in a string, I noticed using a “\w*” pattern lead to 2 matches:

  1. One for the string
  2. Another for the void at the end of the string

For instance: see:

The explanation is at:

In short, we should use “\w+” instead of “\w*” so that zero-length matches are avoided.

Note: if you make the quantifier lazy as in then you have 3 matches.





Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">