Why is the star quantifier greedier than the plus quantifier in Java regular expressions?

Viewed 179

I have text I'm trying to extract from LogicalID and SupplyChain from

 <LogicalID>SupplyChain</Logical>

At first I used the following regex:

.*([A-Za-z]+)>([A-Za-z]+)<.*

This matched as follows:

["D", "SupplyChain"]

In a fit of desperation, I tried using the asterisk instead of the plus:

.*([A-Za-z]*)>([A-Za-z]+)<.*

This matched perfectly.

The documentation says * matches zero or more times and + matches one or more times. Why is * greedier than +?

EDIT: It's been pointed out to me that this isn't the case below. The order of operations explains why the first match group is actually null.

3 Answers
Related