Solved

Regular Exp & Matching Question

Posted on 2002-04-28
1
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

623 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