I’m trying to write a regular expression that surrounds ‘http’ URLs with angle brackets, except for lines beginning with two slashes. The best I’ve come up with is:
s#^(?!//)(.*?)(http://[^\s]+)#$1<$2>#gm;
This works great for these two:
Input: http://a.com
Output: <http://a.com>
Input: //http://a.com
Output: //http://a.com
However, it fails here:
Input: http://a.com http://b.com
Actual Output: <http://a.com> http://b.com
Desired Output: <http://a.com> <http://b.com>
Why doesn’t my regular expression keep matching? Am I using /g wrong?
rewriting it a little…with my suggestions and using the whitespace modifier so it’s actually readable. 🙂
Trying this in perl, we get:
Edit: Couple of changes: Added the ‘m’ modifier to make sure that it matches from the start of a line, and change \G to (^|\G) to make sure it starts looking at the start of the line too.