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

i want to extract only the first url which is before the | , http://someurlhere.com

i have written regular expression

\bhttp(.*)\|\b

but its capture all the occurences of |

Thanx help is appreciated

Fawad
  • 137
  • 1
  • 9
  • 3
    Not really an answer to your question; but consider using `explode()` (http://php.net/manual/en/function.explode.php) – user254875486 Oct 18 '12 at 11:48
  • I don't understand what you're trying to achieve. You're trying to get the first part of text that is an URL (eg. prefixed with http://)? Does the http:// have to be part of the resulting match? Because as of now, your match would be `://someurlhere.com`. – bummzack Oct 18 '12 at 12:02

3 Answers3

9

Make the .* ungreedy : .*?

\bhttp(.*?)\|\b

If you want to match only after the first | do:

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

with this, the url is in $1

Toto
  • 89,455
  • 62
  • 89
  • 125
  • sorry i tried this one ,its works but for this kind http://someurlhere.com|http://sometexturl.com|sometexthere not working – Fawad Oct 18 '12 at 11:53
  • @Fawad: What do you obtain with your example? – Toto Oct 18 '12 at 11:58
  • Thanx got it , i was using global regular expression , thanx again . can u do me one more help . how can i extract only url becuase of this pattern | is also coming in match :) – Fawad Oct 18 '12 at 12:01
  • i have edited the main question can u please read again thanx – Fawad Oct 18 '12 at 12:12
  • @Fawad: with my second regex, the url is in the first capture group `$1` – Toto Oct 18 '12 at 12:33
1

This isn't a exact answer, but I will point you towards information that help you solve this issue!

Regex: matching up to the first occurrence of a character

Community
  • 1
  • 1
Hultin
  • 148
  • 1
  • 2
  • 9
1

No need for regex. I see you tagged it PHP.

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

Output:

http://someurlhere.com

http://codepad.org/xnW0FTfA

ohaal
  • 5,208
  • 2
  • 34
  • 53