From: Sam Kong Date: 2006-12-21T10:14:48+09:00 Subject: Re: Regex question: this should be easy but doesn't work as I expect. Hi Carlos, Carlos wrote: > Hi. The problem is that you can very easily NOT match "three" even if it's > there. I mean, if you have > /1.*3?.*5/ =~ '12345' > > the engine can succeed matching the 1 at the beginning, the 5 at the end, > and trying to match the 3 where the 4 is... and failing, but since it's > optional, the overall match succeeds. > > I think you should try it in two steps: first, try to match with the 3; if > that fails, without the "3". Something like: > > /(?:(1).*(3)|(1)).*(5)/ > > (The '1' will come either on the first or third array position, you'll have > to take care of that.) Yes, you understand exactly what my problem is. Actually I guessed it as you said even if I couldn't explain it as well as you did. The solution I found was using 2 regexes. First, I try to find a match assuming "three" is there. If it fails, I try to find a match without "three". This solved my problem. But I wanted to know that if there's a one-shot solution. This is the actual problem, just in case someone wants to know. html = <
2004 Used
BMW 325Ci Coupe
38,604
Mileage $24,995
Price
0 mi
from ZIP
 
Actual Photo Available 9 Photos

Color - Mystic Blue Metallic

Dark Blue/Beige, Premium Pkg, Xenon Light, Single Compact Disc, Dual Power Seats, Memory Seat, Still under Free BMW Maintenance and 4yr/50k Factory...

VIN WBABV13454JT20104

 

null


END def parse_row row m = row.scan(/.+?
(\d{4}) Used<\/h5>.+?
.+?(.+?)<\/a>
.+?<\/h5>.+?([0-9,]+)
<\/span>Mileage<\/span>.+?(\$[0-9,]+).+?(http:\/\/[^\"]+?\.jpg).+?Color - (.+?)<\/p>/m) if m[0].nil? m = row.scan(/.+?
(\d{4}) Used<\/h5>.+?
.+?(.+?)<\/a>
.+?<\/h5>.+?([0-9,]+)
<\/span>Mileage<\/span>.+?(\$[0-9,]+).+?(http:\/\/[^\"]+?\.jpg)?.+?Color - (.+?)<\/p>/m) end m[0] end p parse_row(html) Sorry about the messy code. Thanks. Sam