0

I'm writing program that replace link with bbcode. I need a regex that replace url with bbcode, example:

before

http://mediafire.com/abc (or http://dropbox.net/abc,...)

I want to filter that name of link also

after

[url=http://name-of-link.com/abc]http://name-of-link.com/abc[/url]

This is what I have got so far:

Search for:

(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?

Replace with:

[url=$1]$2[/url]

But it seems doesn't work. Hope anyone can help me out this issue. Thank you.

Edit:

My issue that I want to filter name of link also, because I don't want to replace link of image to bbcode.

The Hung
  • 309
  • 4
  • 18
  • possible duplicate of [How to replace plain URLs with links?](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links) – Marc B Sep 17 '15 at 16:22
  • @MarcB No mate, My issue that I want to filter name of link also, because I don't want to replace link of image to bbcode – The Hung Sep 17 '15 at 16:29
  • 1
    You forgot to mention that in the question. Generally images are not recognizable from the URL itself. You can only guess that urls ending in e.g. `.jpg` or `.png` are probably images, but they don't have to be. – Wormbo Sep 17 '15 at 16:36
  • what is a name of link? this is not clear. – perreal Sep 17 '15 at 16:36
  • @perreal mediafire or dropbox – The Hung Sep 17 '15 at 16:37
  • @Wormbo Yes, could you please help me? – The Hung Sep 17 '15 at 16:38
  • 1
    IMO you need to write an actual example of what you want. In the above the expected result is confusing. – perreal Sep 17 '15 at 16:47
  • @TheHung Don't know the language you're using, but can you try my generic shortcode processing library: https://github.com/thunderer/Shortcode ? Ping me if you need any help. – Tomasz Kowalczyk Oct 22 '15 at 08:27

1 Answers1

0

Check out this one:

^((http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-]))

replace (bb code)

- [url]($1)[/url]

replace (markup)

- [$1]($1)
rapttor
  • 396
  • 3
  • 7