![]() |
|
|
|||||||
![]() |
PERL - Re: Capture only first match in regular expression |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |