Categories
Regular Expressions

Regular Expressions: Greedy vs. Reluctant Quantifiers

The other day I wanted to extract a part of a filename and was quite dumbfounded when the extracted part came back empty. The regular expression used to extract the part using a capturing group was:

[A-Za-z0-9]*_?([0-9]*)\..*

The filenames fed into this expressing where mostly of the pattern someText_12345.data. However, occasionally a file named 12345.data would pass along and even though its name matched the expression, the capturing group came up empty. But… but… why?