Quote:
Originally Posted by Psymon
I'm not quite sure that I understand the "? : but as few as as possible, as long as the rest of the expression still matches" part, though -- in what sense as few as possible? Couldn't it go on (and on and on, ad infinitum) until it hits the next part of the matching string -- wouldn't it do that without the "?" in there anyway, just stop at that part (the next matching bit)? 
|
The '?' prevents the regex from being greedy...in cases where there are multiple possible matches it will take the first or shortest one.
eg
string searched:
"Hello my name is Inigo Mantoya, son of Hector Mantoya."
find: "name is (.+) Mantoya" or "name is (.*) Mantoya"
is a greedy find...it will match as much as possible and still meet the criteria: "name is Inigo Mantoya, son of Hector Mantoya"
find: "name is (.+?) Mantoya" or "name is (.*?) Mantoya"
is a non-greedy find... it will match on only the first qualifying match "name is Inigo Mantoya"
Hopefully that's clearer than mud!