regex - Matching URLs with other characters around -
i need regex pattern match urls in complicated environment.
an url in position:
[url=http://www.php.net/manual/en/function.preg-replace.php:32p0eixu]test[/url:32p0eixu] (that's sample url)
i need match url until colon, colon , code after should ignored. there many urls out there , i'm not experienced create pattern match http:// :
as said, else should ignored, left away, except url need store in variable.
could me create such pattern? tries matching url above, when put in more complicated urls, wouldn't match.
this pattern i've created. works simple urls, not complicated ones:
http(s)?://[a-za-z0-9.,/_-]+ i'm not in regex, i'm still learning.
thank you.
this regex should you.
\[url=(.*?):[a-za-z0-9]*\] run against test data:
[url=http://www.php.net/manual/en/function.preg-replace.php:32p0eixu]test[/url:32p0eixu] this return url in capture group 1.
assuming php (since test url php manual), you'd use preg_match this:
$value = "[url=http://www.php.net/manual/en/function.preg-replace.php:32p0eixu]test[/url:32p0eixu]"; $pattern = "/\[url=(.*?):[a-za-z0-9]*\]/"; preg_match($pattern, $value, $matches); echo $matches[1]; output:
http://www.php.net/manual/en/function.preg-replace.php this work against urls contain colons in them, such as:
http://www.php.net:8080/manual/en/function.preg-replace.php http://www.php.net/manual/us:en/function.preg-replace.php
Comments
Post a Comment