Eric Daspet a écrit :
> Le Mar 5 décembre 2006 12:36, Michael Hulet a écrit :
>   
>>     
>> page_content.scan(/href\s*=\s*[\"']([^\"']+)\.(wmv|avi|mpeg|mpg|mov|mp4)[\"']>(.+)<\/a>/i).each
>>     
>> Imaginons un code HTML de ce type:
>>
>>     <a href="01.mov"><img src="01_screenshot.jpg" /></a><a
>>     href="02.mov"><img src="02_screenshot.jpg" /></a><a
>>     href="03.mov"><img src="03_screenshot.jpg" /></a>
>>
>> Le pattern ne prend pas bien et me renvoie un tableau avec ce résultat:
>>
>>     a[2] = <img src="01_screenshot.jpg" /></a><a href="02.mov"><img
>>     src="02_screenshot.jpg" /></a><a href="03.mov"><img
>>     src="03_screenshot.jpg" />
>>     
> Les expressions rationnelles sont dites "gourmandes" par défaut. Le ".+"
> c'est "un caractère, n'importe lequel, autant de fois que possible". Du
> coup, si tu vérifies, ce qu'il te sort correspond bel et bien à ce que tu
> lui demandes. Il t'a trouvé un <a href="...">, un truc au milieu, et un
> </a> à la fin.
>
> Si tu veux que ton ".+" capture le strict minimum, c'est à toi de l'en
> informer. Changes ton ".+" par un ".+?", et ça devrait faire ce que tu
> souhaites.
>   
Fantastique, c'est tout bon. Mon sauveur! :)

Au passage, j'apprends donc l'usage du '?' dans les expressions
régulières: "0 or 1 of previous expression; forces minimal matching when
an expression might match several strings within a search string".

Michael

begin:vcard
fn:Michael Hulet
n:Hulet;Michael
org:mindtheshark.com
adr;quoted-printable:;;chauss=C3=A9e de Bruxelles, 11;Wavre;;1300;Belgique
email;internet:[EMAIL PROTECTED]
title;quoted-printable:Cr=C3=A9ateur web ind=C3=A9pendant
tel;cell:+32498119278
note;quoted-printable:MSN Messenger: [EMAIL PROTECTED]
	
url:http://www.mindtheshark.com
version:2.1
end:vcard

_______________________________________________
Railsfrance mailing list
[email protected]
http://lists.rubyonrails.fr/mailman/listinfo/railsfrance

Répondre à