Dmytro Krasun
Dmytro Krasun

Reputation: 1742

RegEx: URI parts from text

all! I have a text like this "Some text with uri http://test.com and other words.". And I need to get parts of uri using one regular expression.

I try this:

string text = "Some text with uri http://test.com and other words.";
string pattern = @"\b(\S+)://([^:]+)(?::(\S+))?\b"; 
MatchCollection matches = Regex.Matches(text, pattern); 

And it works, when i write "Some text with uri http://test.com" or "word1 http://test.com:5000 word2".

Where is mistake?

Upvotes: 3

Views: 441

Answers (2)

Justin Morgan
Justin Morgan

Reputation: 30700

Your second + modifier is greedy, so it's matching everything after the http:// unless it hits a : or the end of the line. Try this:

@"\b(\w+)://([^:]+?)(?::(\S+))?\b"

Upvotes: 1

John Sobolewski
John Sobolewski

Reputation: 4572

This should get you closer... I"m still not exactly sure what you want to get...

If you could show the results you want it would help...

\b(\S+)://([^: ]+)

Upvotes: 0

Related Questions