Reputation: 535
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
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
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