RE: How to avoid this greedy match?



Try out the following regular expression:

perl -e '$str = "...<script>442226</script>zzz<script>222</script>...";print "1=$1\n" if ($str =~ /.+<script>(.*222.*)<\/script>/);'

The regeular expression is: .+<script>(.*222.*)<\/script>

This might give a strange results if you have multiple '222' in the input string. Regex will always pick up the last occurance of '222'.

~Ajay

-----Original Message-----
From: News Howardz [mailto:news_howardz@xxxxxxxxx]
Sent: Tuesday, January 29, 2008 9:36 AM
To: beginners@xxxxxxxx
Subject: Re: How to avoid this greedy match?


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?

Thanks
Howardz

----- Original Message ----
From: News Howardz <news_howardz@xxxxxxxxx>
To: beginners@xxxxxxxx
Sent: Tuesday, January 29, 2008 11:22:41 AM
Subject: How to avoid this greedy match?

Hi,

I have a trouble when composing a regex.

$str = "...<scirpt>xxx</script>zzz<script>yyy</script>..."

In the above string, "xxx" and "yyy", "zzz" stand for any substring other than "<script>"

I want to pick up "<script>yyy</script>", but each time I get the 2 script sections matched "<scirpt>xxx</script>zzz<script>yyy</script>".

I've tried the following regex:
1) "<script>.*</script>
2) "<script>.*?</script>

How can I avoid the this greedy match?
(I'm using ActivePerl 5.6.1)

Thanks,
Howardz


____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/


____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/


____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/


.



Relevant Pages

  • Re: How to avoid this greedy match?
    ... How to avoid this greedy match? ... I have a trouble when composing a regex. ... Make Yahoo your home page. ...
    (perl.beginners)
  • Re: regular expression question
    ... This is a regex which automatically uses whatever is ... Do You Yahoo!? ... Mail has the best spam protection around ...
    (perl.beginners)
  • Re: help with a regex and greediness
    ... My mistake, I'll check it out. ... That's why I was using a regex. ... > format (which is no different than the requirement ... Do you Yahoo!? ...
    (perl.beginners)
  • Re: Yet Another Regex Problem
    ... > this regex are goin' to drive me crazy! ... Try the all-new Yahoo! ...
    (perl.beginners)
  • Re: help with a regex and greediness
    ... most of my regex and incorporate split on the string. ... I think it has to do with the comma not being present ... after 'Suns' so I modified the splitto look like ... Do you Yahoo!? ...
    (perl.beginners)