Re: Capture only first match in regular expression

P

Peter Tuente

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top