Ajay & Rob, thanks for your perfect solutions! It does pick up the last matched string if we add another greedy match pattern (.+ or .*) in front of the original regex.
And thanks for the replies from yitzle, John, Ruud as well. BR Howardz ----- Original Message ---- From: Rob Dixon <[EMAIL PROTECTED]> To: beginners@perl.org Cc: News Howardz <[EMAIL PROTECTED]> Sent: Tuesday, January 29, 2008 10:02:48 PM Subject: Re: How to avoid this greedy match? News Howardz wrote: > > The original mail is regarded as a SPAM by Yahoo -- poor regex match :-(. > So I modify the following content and resend it. > ================================ > Sorry, I make a mistake in the mail below: > > $str = "...<script>xxx</script>zzz<script>y222yy</script>..."; > > I want to match the script section containing "222". > So I wrote regex like this: > > /(<script>.*?222.*?<\/script>)/ > > But it doesn't work. > It still selects the 2 script sections: > "<script>xxx</script>zzz<script>y222yy</script>". > > Does anyone have an idea how to achieve this? Is this what you want? It will find the /last/ occurrence of the script section containing 222. Rob use strict; use warnings; my $str = "...<script>xxx</script>zzz<script>y222yy</script>..."; $str =~ /.*(<script>.*?222.*?<\/script>)/; print $1; **OUTPUT** <script>y222yy</script> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/