regextcl

What is the difference between ".*" and ".*?"


I wanted to catch comment on code (everything from "--" to the end of the line) using regular expressions in TCL.

So I tried {\\-\\-.*$} that should be - then - then any number of any characters and then end of the line. But it doesn't work!

Another post here suggested using .*? instead of .*.

So I tried {\\-\\-.*?$} and that works.

Just wanted to understand the difference between the two. According to any regular expression tutorial/man I read the ? condition should be a subset of *, so I am wondering what's going on there.

And yes my question is about ".? vs .", it is not the same as ".+? vs .*?" so i don't see in what world those two can be a "duplicate" ????


Solution

  • "?" makes de previous quantifier lazy, making it match as few characters as posible.