开发者

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://t开发者_C百科est.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?


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"


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+)://([^: ]+)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜