From: Logan Capaldo Date: 2007-06-10T21:21:51+09:00 Subject: Re: regex: get the first match ------=_Part_43175_7396553.1181478112576 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 6/10/07, Robert Dober wrote: > > On 6/10/07, Trochalakis Christos wrote: > > Hello! > > > > I want to parse a tagged string like this: "this ismy > > string" > > > > i am doing: > > > > >> "this ismy string".scan(/(.*)<\/i>/) > > => [["this ismy string"]] > > > > What i want is a regex that will return the *first* segment that > > matches. > > in the above case -> [["this is", "my string"]] > > > > Is there any way to do this? > > > > Thanks! > > > > > > > This is a FAQ, and yes I will give the solution ;) > Regexps are gready par default, they consume as many chars as > possible, there are some possibilities - not tested: > > (1) use non gready matches > "this ismy string".scan(/(.*?)<\/i>/) > (2) use less general expressions > "this ismy string".scan(/(.[^<]*)<\/i>/) > (3) Combine both ;) > "this ismy string".scan(/(.[^<]*?)<\/i>/) .Unless you want to match strings like , it would be simple to just use [^<]*, and not .[^<]*. .[^<]* will also not match . If the intent was to make the regexp not match that, a better regexp would be [^<]+ HTH > Robert > > P.S. > This *really* is a FAQ though > -- > You see things; and you say Why? > But I dream things that never were; and I say Why not? > -- George Bernard Shaw > > ------=_Part_43175_7396553.1181478112576--