Solved

Regular Exp & Matching Question

Posted on 2002-04-28
1
187 Views
Last Modified: 2010-03-05
Everytime I use perl i some how wind up needing to match the smallest occurence of a substr within a string.

$a="< a hhref='http://www.yahoo.com' name=a>Yahoo!</a><a href ='link.com'>Link</a>";
if ($a=~/name=a>(.*)<\/a>/)
{
   print $1;
}

The result I want is "Yahoo!" but what i actually get is "Yahoo!</a><a href ='link.com'>Link".  I realize the regular expression ".*" is the culprit but how do i tell perl to grab the "smallest" matching string and not the default largest matching string?  Is it simply replacing ".*" with "[^<]*" for my case or is there another method?
0
Comment
Question by:barkin
1 Comment
 
LVL 25

Accepted Solution

by:
clockwatcher earned 50 total points
ID: 6975311
'?' will make the match non-greedy.

$a="< a hhref='http://www.yahoo.com' name=a>Yahoo!</a><a href ='link.com'>Link</a>";
if ($a=~/name=a>(.*?)<\/a>/)
{
  print $1;
}

See:

 perldoc -q greedy
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now