How .* (dot star) works?

the dot means anything can go here and the star means at least 0 times so .* accepts any sequence of characters, including an empty string.


Each case is different:

.*([a-m\/]*).*

The first .* will probably match the whole string, because [a-m/] is not required to be present, and the first * is greedy and comes first.

.*([a-m\/]+).*

The first .* will match the whole string up to the last character that matches [a-m/] since only one is required, and the first * is greedy and comes first.

.*?([a-m\/]*).*

The first .*? will match the string up to the FIRST character that matches [a-m/], because *? is not greedy, then [a-m/]* will match all it can, because * is greedy, and then the last .* will match the rest of the string.


The function of .* in your examples is to make sure that the containing expression could be surrounded with anything (or nothing). The dot represents an arbitrary character, and the asterisk says that the character before can be repeated an arbitrary number of times (or not at all).

Tags:

Regex