Solved

How to extract an IP using "word boundary"

Posted on 2015-02-10
5
97 Views
Last Modified: 2015-02-10
Hi, I have a very simple regex to find IP addresses in this format: 3.123.210.211

\d.\d\d\d.\d\d\d.\d\d\d

When presented with a string that contains something like 135.231.351.209, it will capture 5.231.351.209, but i don't want it to. I only want it to capture the IP's that come in the form x.xxx.xxx.xxx. I had a look at the "word boundary" option (\b) and it seems like it's what I need, but I'm foggy on the usage of it. Could anyone shed some light please?

Thanks
   Shawn
0
Comment
Question by:shawn857
  • 3
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I could have sworn I'd previously answered a question similar to this, but I can't find it now...  oh well  = )

You can actually match valid IP address ranges with regex. The following will do this:

\b(25[0-5]|2[0-4]\d|1\d\d|\d?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|\d?\d)){3}\b

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
P.S.

On second glance, based on what is in your question, you probably just need to add the word boundary token ( \b ):

\b\d.\d\d\d.\d\d\d.\d\d\d\b

Open in new window

0
 

Author Comment

by:shawn857
Comment Utility
Thanks Kaufmed... so do I need a "\b" at the start of my regex and another "\b" at the end to do what I want? Doesn't this activate the "word boundary" capabilities at the beginning *and* the end? if I only want to have "word boundary" activated for the beginning, then I would just put a "\b" at the start of my regex? Or do I always have to have a "\b" on the end? I'm still a little confused about its usage.

Thanks
   Shawn
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
You don't have to have it at the end, but without it there is the possibility that you might match something more than you want. For example, if you had the following blurb:

This is a test of a paragraph that has a bunch of octets, like 1.234.567.890.123, but the octets don't represent an IP address.

Then a regex like "\b\d.\d\d\d.\d\d\d.\d\d\d" would match:

1.234.567.890

If you're sure you won't have anything like this in your text, then you don't need the trailing word boundary. It shouldn't hurt having it in there.
0
 

Author Closing Comment

by:shawn857
Comment Utility
Thanks you Kaufmed, I now have this working perfectly.

Cheers
   Shawn
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now