Go Back   Velocity Reviews > Newsgroups > PERL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

PERL - Re: Capture only first match in regular expression

 
Thread Tools Search this Thread
Old 04-17-2009, 12:36 PM   #1
Default Re: Capture only first match in regular expression


Hi Zapanaz,

the default behaviour of regular expression terms is to be "greedy", so to
suppress this behaviour to become "not greedy" you have to apply a single
question mark "?" right after the desired expression(s). Sounds some kind of
complex, but I hope you get me

In your case the following should be sufficient:

# old: if($content =~ /.*(<a.*<\/a>).*/i){
$anchorContent = $1;

# new:
if($content =~ /.*?(<a.*?<\/a>).*/i){
$anchorContent = $1;

The effect is, that the first expression ".*" becomes not so greedy eating
all the possible chars (incl. one/some "<a" chars that prefix the last
occurrence of "<a" in the current line). Same with the second ".*".

Hope this helps

Bye.

PiT

"Zapanaz" <http://joecosby.com/code/mail.pl> schrieb im Newsbeitrag
news:...
> Excuse the cross-post, my server doesn't carry comp.lang.perl.misc but
> it looks like there is more activity there.
>
>
> The answer to this is probably staring me in the face ...
>
> I am parsing/page scraping some HTML. I know the first anchor tag <a>
> contains information I want.
>
> So I do this:
>
> if($content =~ /.*(<a.*<\/a>).*/i){
> $anchorContent = $1;
>
> This basically works the way I want, it matches an anchor tag and
> captures the content of it.
>
> But there are multiple anchor tags in the HTML. What I want is the
> first one, but what I get is the last one.
>
> I think I should be using one of these
>
> * Match 0 or more times
> + Match 1 or more times
> ? Match 1 or 0 times
> {n} Match exactly n times
> {n,} Match at least n times
> {n,m} Match at least n but not more than m times
>
> To be honest, I really don't know how (n) is actually supposed to
> look. Would I actually use /a(1)/ to match "a" only one time?
>
>
>
> --
> Zapanaz
> International Satanic Conspiracy
> Customer Support Specialist
> http://joecosby.com/
> Despite the strange appearance of the scooters, the Chinese ant-terror
> police are lethal in action.
>
> :: Currently listening to No 21 in C major K467 Allegro maestoso, 1785, by
> Mozart, from "Piano Concertos - Vladimir Ashkenazy"





Peter Tuente
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SuperVideoCap work as a broadcast capture and screen capture and record tool. hely0123 Media 0 10-30-2007 08:59 AM
Need help on Modelsim VHDL syntax? ASAP:) kaji General Help Related Topics 0 03-14-2007 10:43 PM
Need help on a Modelsim VHDL Syntax? ASAP:) kaji Software 0 03-14-2007 10:43 PM
Need Help on a Modelsim VHDL Syntax....ASAP:) kaji Hardware 0 03-14-2007 10:41 PM
Capture Card and Software Advice Scott DVD Video 1 04-18-2004 08:39 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46