alexy12
alexy12

Reputation: 535

Regex to match Ip:Port

I have the following regex to match Ip:Port form html code, but some some reason I'm only getting the first match returned, then it stops.

my code:

       Match m = Regex.Match(_theHtmlCode, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,8}\b", RegexOptions.IgnoreCase);

       if (m.Success)
       {
           if(m.Groups[0].Value != "")
           {
               resultsFound.Add(m.Groups[0].Value);
           }

       }

Any ideas how i could get it to add all of the matches into resultsFound?

Upvotes: 0

Views: 2391

Answers (2)

burning_LEGION
burning_LEGION

Reputation: 13450

var m = Regex.Matches(_theHtmlCode, @"\b(\d{1,3}\.){3}\d{1,3}\:\d{1,8}\b", RegexOptions.IgnoreCase); and this regex can get wrong ip, this matches only true ip: ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d+

Upvotes: 5

David M
David M

Reputation: 72930

You need to use the Regex.Matches method not the Regex.Match method. This returns a MatchCollection rather than an individual Match, with the collection containing all matches for the regular expression.

Upvotes: 0

Related Questions