Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 don't know where to place the {1,3}?

Please help me.

Thank you.

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...