RegEx: URI parts from text

all! I have this text: "Some text from uri http://test.com and other words.". And I need to get uri parts using one regex.

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/10000 word2".

Where is the mistake?

+3
source share
2 answers

Your second modifier +is greedy, so it matches everything after http://if it doesn't fall at :or at the end of the line. Try the following:

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

... , ...

, , ...

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

Source: https://habr.com/ru/post/1791623/


All Articles