PHP正则表达式匹配第一次出现?

时间:2012-10-18 11:45:38

标签: php regex

http://someurlhere.com|http://sometexturl.com|sometexthere

我想只提取在|之前的第一个网址,http://someurlhere.com

我写了正则表达式

\bhttp(.*)\|\b

但它捕获了|

的所有出现

感谢Thanx帮助

3 个答案:

答案 0 :(得分:8)

.* ungreedy:.*?

\bhttp(.*?)\|\b

如果您只想在第一个|之后进行匹配:

\|http://(.*?)\|

有了这个,网址在$1

答案 1 :(得分:1)

这不是一个确切的答案,但我会指出有助于您解决此问题的信息!

Regex: matching up to the first occurrence of a character

答案 2 :(得分:0)

不需要正则表达式。我看到你把它标记为PHP。

$boom = explode('|', 'http://someurlhere.com|sometexturl.com|sometexthere');
$url = $boom[0];
echo $url;

输出:

http://someurlhere.com

http://codepad.org/xnW0FTfA