Solved

Substr

Posted on 1998-09-05
4
279 Views
Last Modified: 2010-07-27
I have some srings like
<br><a href="test.htm"><b>Some Text</b></a>
and
<br><a href="testgetmore.htm"><b>More Text</b></a>

i want to get the substring after <b> and before </b> but the position of "<b>" is on a defrent position in the string
how do i get the position of the <b> and how do u use substr?

Thanks

0
Comment
Question by:delpro
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
($substring) = $string =~ m|<b>(.*?)</b>|;

#to get the position of the <b>
$position = index $string,'<b>';
#to use substr
$substring = substr $string,$position+3;
#or
$substring = substr $string,$position+3,(index $string,'</b>')-$position-3;
0
 
LVL 28

Accepted Solution

by:
sybe earned 0 total points
Comment Utility
sub ExtractBetween
{
  local($string, $tag1, $tag2) = @_;
  $ind1 = index($string, $tag1) + length($tag1);
  $ind2 = index($string, $tag2);
  $string = substr($string,$ind1,$ind2-$ind1);
  return $string;
}


$yourstring = '<br><a href="test.htm"><b>Some Text</b></a>';
$extractedstring = ExtractBetween($yourstring,'<b>','</b>');

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#If you're going to use it in a sub, then perhaps
 $ind2 = index($string, $tag2);
#would be be safer as
 $ind2 = index($string, $tag2, $ind1);
#so that
ExtractBetween('</b><br><a href="test.htm"><b>Some Text</b>',<b>','<');
#still works

#although for literal tags,
($extractedstring) = $yourstring =~ m|<b>(.*?)</b>|;
#still seems easier
0
 
LVL 28

Expert Comment

by:sybe
Comment Utility
I wrote my answer as ozo posted his comment, so i did not see his first comment.
Anyway, I agree with ozo that his answer is easier.

ozo, you're a real perl-magician !

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

15 Experts available now in Live!

Get 1:1 Help Now