View Single Post
Old 07-14-2016, 09:40 AM   #41
Turtle91
A Hairy Wizard
Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.
 
Turtle91's Avatar
 
Posts: 3,361
Karma: 20212223
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
Quote:
Originally Posted by Psymon View Post
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!
Turtle91 is offline   Reply With Quote