2.20. Insert the Regex Match into the Replacement Text


Perform a search-and-replace that converts URLs into HTML links that point to the URL, and use the URL as the text for the link. For this exercise, define a URL as “http:” and all nonwhitespace characters that follow it. For instance, Please visit http://www.regexcookbook.com becomes Please visit <a href="http://www.regexcookbook.com">http://www.regexcookbook.com</a>.


Regular expression

Regex options: None
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby


Replacement text flavors: .NET, JavaScript, Perl
Replacement text flavors: .NET, Java, XRegExp, PHP
Replacement text flavors: PHP, Ruby
Replacement text flavor: Ruby
Replacement text flavor: Python


Inserting the whole regex match back into the replacement text is an easy way to insert new text before, after, or around the matched text, or even between multiple copies of the matched text. Unless you’re using Python, you don’t have to add any capturing groups to your regular expression to be able to reuse the overall match.

In Perl, «$&» is actually a variable. Perl stores the overall regex match in this variable after each successful regex match. Using «$&» adds a performance penalty to all your regexes in Perl, so you may prefer to wrap your whole regex in a capturing group and use a backreference to that group instead.

.NET and ...

Get Regular Expressions Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.