What is the difference between (. *) And (. *?) In regular expressions?

advertisements

This question already has an answer here:

  • What do lazy and greedy mean in the context of regular expressions? 8 answers
  • Differences between (.*) and (.*?) [duplicate] 4 answers

What is the difference between (.*) and (.*?) in regular expressions?

I used the following string:

<select name="show">
    <option value="all"      >all php.net sites</option>
    <option value="local"    >this mirror only</option>
    <option value="quickref" selected="selected">function list</option>
    <option value="manual"   >online documentation</option>
    <option value="bugdb"    >bug database</option>
    <option value="news_archive">Site News Archive</option>
    <option value="changelogs">All Changelogs</option>
    <option value="pear"     >just pear.php.net</option>
    <option value="pecl"     >just pecl.php.net</option>
    <option value="talks"    >just talks.php.net</option>
    <option value="maillist" >general mailing list</option>
    <option value="devlist"  >developer mailing list</option>
    <option value="phpdoc"   >documentation mailing list</option>
</select>

Regular Expression : /<option\svalue=(.*)>/s

Returns one result with a single string containing all the option values.

Regular Expression: /<option\svalue=(.*?)>/s

Returns 13 results.

By observing the output I find .* searches from the end and .*? searches from the beginning is this a correct assumption?


‍‍.*? matches all the characters until the next pattern after .*? is found. But .* just matches all the characters.

In hellohello, h.*o will match hellohello. But h.*?o will match only hello.

.* is called greedy and .*? is called non-greedy.