Skip to content


Regex lazy match

Thought I’d write about the Regex lazy match here while I still have it in a explainable state in my mind…

For example, consider the string:

[test string] [test string]

With the regex:

\[.+\]

The + operator is greedy, and will attempt to match as much text as possible, which would produce a single match group containing [test string] [test string]

To stop this behavior, use the lazy plus: +?

\[.+?\]

This produces two match groups: [test string] [test string]

 

Posted in Information, Java. Tagged with , .