How to get the inverse of a regular expression?

boysenberry picture boysenberry · Jul 17, 2009 · Viewed 9.4k times · Source

Let's say I have a regular expression that works correctly to find all of the URLs in a text file:

(http://)([a-zA-Z0-9\/\.])*

If what I want is not the URLs but the inverse - all other text except the URLs - is there an easy modification to make to get this?

Answer

dmcer picture dmcer · Jul 17, 2009

You could simply search and replace everything that matches the regular expression with an empty string, e.g. in Perl s/(http:\/\/)([a-zA-Z0-9\/\.])*//g

This would give you everything in the original text, except those substrings that match the regular expression.