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-12-2009, 03:40 AM   #1
Default Re: Capture only first match in regular expression


Zapanaz <http://joecosby.com/code/mail.pl> wrote:
>
>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.


Drop that .* at the beginning of your RE, it doesn't do you any good but
eats up everything as far as it can provided the following RE still
matches (in short: it is greedy).

Having said that unless your HTML is some fixed format you really
really should be using an HTML parser to parse HTML. HTML is not a
regular language and therefore cannot be parsed using pure regular
expressions.

>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


If at all you could use ? to turn the * into non-greedy as in .*?, but
that's just stupid because it would match the empty string anywhere.

jue


Jürgen Exner
  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