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

2
  • 3
    Not really an answer to your question; but consider using explode() (php.net/manual/en/function.explode.php) Commented Oct 18, 2012 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. Commented Oct 18, 2012 at 12:02

3 Answers 3

9

Make the .* ungreedy : .*?

\bhttp(.*?)\|\b

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

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

with this, the url is in $1

Sign up to request clarification or add additional context in comments.

5 Comments

sorry i tried this one ,its works but for this kind someurlhere.com|http://sometexturl.com|sometexthere not working
@Fawad: What do you obtain with your example?
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 :)
i have edited the main question can u please read again thanx
@Fawad: with my second regex, the url is in the first capture group $1
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.