tags:

views:

62

answers:

2

i have this string

[url=test.php]test[/url]

or

[url=http://test.php]text[/url]

I want to a regex get this

[url=http://test.php]text[/url]

example:

before:

str = "[url=test.php]text[/url]"
str.gsub(/\[url=(.*?)\](.*?)\[\/url\]/,'[url=http://\1]\2[/url]')
=> "[url=http://test.php]text[/url]"

question:

str = "[url=http://test.php]text[/url]"
str.gsub(/\[url=(.*?)\](.*?)\[\/url\]/,'[url=http://\1]\2[/url]')
=> "[url=http://http://test.php]text[/url]"

so.. two "http://"

Any ideas anyone?

thanks

A: 

Thanks for clarifying your question.

You need a regex that only matches [url] BBCode tags if the linked URL is not starting with http.

\[url=\s*(?!http)([^\]]+)\](.*?)\[/url\]

should work:

str.gsub(/\[url=\s*(?!http)([^\]]+)\](.*?)\[\/url\]/,'[url=http://\1]\2[/url]')

Explanation:

\[url=\s*   # match [url= literally, plus optional space
(?!http)    # assert that it's not possible to match http here
([^\]]+)    # match 1 or more characters except ], capture in \1
\]          # match ]
(.*?)       # match link text, capture in \2
\[/url\]    # match [/url]
Tim Pietzcker
i already update my question
squarezw
A: 

Thanks Tim Pietzcker.

Sorry my english is poor.

My code is

f="[url=test.php]test[/url]"
f.gsub(/\[url=http[^]]+\].*?\[\/url\]/, "")

if I get add "http://" tag but maybe the "http://" already existed.

so...

squarezw
Hi squarezw, welcome to StackOverflow! You shouldn't put this in an answer (after all it isn't an answer) but rather edit your question. Although I still don't understand what you mean. Maybe you could provide some examples (before and after - what you do and don't want to match)?
Tim Pietzcker