I will underline only the regex expression as it is very important to get the urls. So it will be:
(?!\[url\])\s+\bhttp:\/\/stackoverflow.com\/\d\s+(?<!\[\/url\])
you can see the result in this Url by using the php function preg_match_all
but before that let's understand every part of it (you can find this in the same site)
(?!\[url\])\s+\bhttp:\/\/stackoverflow.com\/\d\s+(?<!\[\/url\])
\s+ match any white space character [\r\n\t\f ]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
http: matches the characters http: literally (case sensitive)
\/ matches the character / literally
\/ matches the character / literally
stackoverflow matches the characters stackoverflow literally (case sensitive)
. matches any character (except newline)
com matches the characters com literally (case sensitive)
/ matches the character / literally
\d match a digit [0-9]
\s+ match any white space character [\r\n\t\f ]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?<!\[\/url\]) Negative Lookbehind - Assert that it is impossible to match the regex below
\[ matches the character [ literally
\/ matches the character / literally
- url matches the characters url literally (case insensitive)
\] matches the character ] literally
Finally you need to use the php function as follow:
preg_match_all("(?!\[url\])\s+\bhttp:\/\/stackoverflow.com\/\d\s+(?<!\[\/url\])", $input_lines, $output_array);
$input_lines is the variable that holds your string
$output_array the arrays that holds the urls
http? See my answer below if so.