How do I do a replace that will turn all plain addresses in a paragraph in to links?
The problem is periods are valid in the address, but the address may be at the end of a sentence.
Test string:
The link is: http://www.google.com/pants. And that is the link.
I need to group out from http to pants. (It is plain text and I need to make it html.)
This regex grabs the period after pants and so is bad:
(^|[\n ])([\w]+?://[^\s]*)
I’m pretty sure I need to do a lookahead, but I can’t put that in the ^\s character set. Trying to do an ifthenelse has also eluded me.
Here is my output thingy:
$1<a href=\"$2\" target=\"_blank\">$2</a>
Hey, people reading this. Make sure you know this site: http://gskinner.com/RegExr/ It rules. It’s the only reason I get any regex right.
Assuming no space in the urls and a space or end of string after them:
It captures ‘http[s]://’ and non-space characters as few times as possible until looking ahead there is optionally
.and then a space or the end of string.If you want to exclude other punctuation that may be at the end of an url you could change the positve lookahead accordingly, e.g.
(?=[;:!,.]?(?:\s|$)).Note that the above regex is not intended to only match valid urls, and you may want to replace the
\Swith[\w/.-]to only match urls containing word characters and.-/.In search of the perfect URL validation regex