0

I'm trying to write a parser that will create links found in posted text that are formatted like so:

[Site Description](http://www.stackoverflow.com)

to be rendered as a standard HTML link like this:

<a href="http://www.stackoverflow.com">Site Description</a>

So far what I have is the expression listed below and will work on the example above, but if will not work if the URL has anything after the ".com". Obviously there is no single regex expression that will find every URL but would like to be able to match as many as I can.

(\[)([A-Za-z0-9 -_]*)(\])(\()((http|https|ftp)\://[A-Za-z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?)(\))

Any help would be greatly appreciated. Thanks.

Andy Evans
  • 6,195
  • 17
  • 62
  • 112
  • 1
    Could you maybe just look for the []() pair rather than trying to find an address? – MikeH Mar 26 '14 at 18:51
  • 1
    Check [this](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) for an example of checking for a valid URL – MikeH Mar 26 '14 at 18:53

2 Answers2

1

Well, you could try negated classes so you don't have to worry about the parsing of the url itself?

\[([^]]+)\]\(([^)]+)\)

And replace with:

<a href="$2">$1</a>

regex101 demo

Or maybe use only the beginning parts to identify a url?

\[([^]]+)\]\(((?:https?|ftp)://[^)]+)\)

The replace is the same.

Jerry
  • 67,172
  • 12
  • 92
  • 128
1

Darn. It seems @Jerry and @MikeH beat me to it. My answer is best, however, as the link tags are all uppercase ;)

Find what: \[([^]]+)\]\(([^)]+)\)

Replace with: <A HREF="$2">$1</A>

http://regex101.com/r/cY7lF0

aliteralmind
  • 18,274
  • 16
  • 66
  • 102