in reply to How do I select text between two bits of text?

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.

Replies are listed 'Best First'.
Re^2: How do I select text between two bits of text?
by JavaFan (Canon) on Dec 04, 2011 at 10:30 UTC
    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.
    I strongly disagree with the sentiment. By all means, if you're paying for your own time, feel free to make the effort to write extremely defensive code, that will last forever.

    Often though, it makes less sense. See, every minute you spend coding for something that may happen between now and the end of the universe, is a wasted minute if said thing never happens, or it doesn't happen until the program is obsoleted. A minute you could have spend writing something that actual scratches a current itch, solves a problem that needs fixing now, or something that will make money right now.

    In this case, I don't think we'll be generating or parsing HTML 4.01 a few decades from now; 75% will be "tag soup, but at least it works on IE 6", 20.9% will work on the designers setup, and only on the designers setup, 4% will not work anywhere, nor does it follow any known standard, and the remaining 0.1% has made an attempt to follow a newer standard.

    That having said, it may (or may not) make sense for the OP to cater for such an element appearing in the HTML text. But without knowing more about the context, I can't say for sure.