Title: How can I replace text that is not part of an anchor tag in Perl?
Tags: regex;perl;text;anchor
Question: What is a Perl regex that can replace select text that is not part of an anchor tag?  For example I would like to replace only the last "text" in the following code.

```blah <a href="http://www.text.com"&gt; blah text blah </a&gt; blah text blah.
```

Thanks.

Comment: @Jay: Presumably he's doing `s/text/replacement/g`, so the blahs don't match.  But this is not a job for a regex (alone).

Comment: Aren't the first and last two "blahs" also "not part of an anchor tag?"

Comment: Ah... got it. Yes, refer to the seminal text on the subject: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

Comment: It is said that in Ulthar, which lies beyond the river Skai, no man may parse html with a regex.

Comment: gulp.  Regex and html.  goes to hide...

Comment: @Jay - I assume the OP wants to `magic_replace(html, 'text', 'link still ok')`

Here is the accepted answer: I have temporarily prevailed:  

```$html =~ s|(text)([^<&gt;]*?<)(?!\/a&gt;)|replacement$2|is;
```

but I was dispirited, dismayed, and enervated by the seminal text; and so shall pursue Treebuilder in subsequent endeavors.

Comment for this answer: Your regex will also replace the "text" inside `text`, because it only looks at the first end tag.
Comment for this answer: Use of regex html parsers will cause you to wind up like Charles Dexter Ward.
Comment for this answer: it depends on what you're parsing - if they are small, regular lines of HTML output by another process for example, then a regex might be appropriate. if they are actual full HTML pages, then a proper HTML parser makes sense...

Here is another answer: You don't want to try to parse HTML with a regex.  Try HTML181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16TreeBuilder instead.

```use HTML181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16TreeBuilder;

my $html = HTML181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16TreeBuilder-&gt;new_from_file('file.html');
# or some other method, depending on where your HTML is

doReplace($html);

sub doReplace
{
  my $elt = shift;

  foreach my $node ($elt-&gt;content_refs_list) {
    if (ref $$node) {
      doReplace($$node) unless $$node-&gt;tag eq 'a';
    } else {
      $$node =~ s/text/replacement/g;
    } # end else this is a text node
  } # end foreach $node

} # end doReplace
```


Here is another answer: Don't use regexps for this kind of stuff. Use some proper HTML parser, and simply use plain regexp for parts of html that you're interested in.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:01/25/10', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<regex><perl><text><anchor>', 'Title': 'How can I replace text that is not part of an anchor tag in Perl?', 'ViewCount': 382, 'id': 2131473, 'user_AboutMe': '', 'user_AccountId': 44349.0, 'user_DisplayName': 'zylstra', 'user_Reputation': 706.0, 'user_Views': 89.0, 'user_id': 130117.0}}