While agreeing with GrandFather on the wisdom of using tried-and-true Modules in the HTML family, and seconding davido's objections to your code, if your use of the <a name=... construct satisfies the HTML 4.01 standard, this should work:
#!/usr/bin/perl use Modern::Perl; my $capture; my $SFS = '<big><a name="hit men for hire" style="font-weight:bold;">H +it Men</a></big>'; # *1 my $regex = qr#<a name="([^"]+)#; # *2 if ( $SFS =~ /$regex/ ) { $capture = $1; say "\$capture: $capture" }else { say "No match"; } # $capture: hit men for hire

*1: Use of a style or span tag, among others, is legit... and a complication for which you'll want to allow unless you can be absolutely positive from now till the heat death of the universe, that no such element will appear in the hmtl you're parsing.

*2: The regex uses a negated character class which matches one or more of anything that is not a double-quote. The definitive end of the text *3 in an <a name="... element is the closing quote which matchs the opening quote. It may be single or double, but must match. So use that fact in your regex; write your regex specifying ONLY the minimum required to satisfy your spec; anything extra is just an invitation to error.

*3: The style component of the tag is, of course, text, but within what I understand to be the common meaning in OP's context, only the quoted portion is "text;" the style component is not what being described.

For a better understanding of regexen, see the relevant section in Tutorials, perlretut and similar.


In reply to Re: How do I select text between two bits of text? by ww
in thread How do I select text between two bits of text? by sdyates

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.