Regular Expressions

ADO DOT NET

Well-known member
Joined
Dec 20, 2006
Messages
156
Hi,
I want to extract all IP addresses using Regular Expressions:
-
Dim MC As MatchCollection = Regex.Matches(ReadText, "([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9])")
-
A valid IP address will be in format: xxx.xxx.xxx.xxx
Each xxx can be from 0 to 255, so it can be from 1 to 3 characters.
I think the above code should be OK.
Just there is a problem that it will accept numbers like 6.2600.5.1 which is invalid.
I know that to force it to accept only 1~3 char numbers I should use {1,3}, yes?
But I dont know where to place the {1,3}?
Please help me.
Thank you.
 
You would put the {1,3} after the item in question i.e. [0-9]{1,3} for each of the octets (also \d[0-9]{1,3} should work as it will match a numeric digit one, two or three times).

Be aware though this will still match numbers larger than 255 but less than 1000 so you might need to either modify the expression to only match 0 - 255 for a range.

What are you doing with the numbers when you get them? Only asking because it might be easier to us a regex to find the things that look like ip addresses and then eliminate the invalid ones via VB / C# later on.
 

Similar threads

Back
Top