Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to extract an IP using "word boundary"

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


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 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?

Question by:shawn857
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40602186
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:


Open in new window

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40602188

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


Open in new window


Author Comment

ID: 40602208
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.

LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 40602215
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:


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.

Author Closing Comment

ID: 40602431
Thanks you Kaufmed, I now have this working perfectly.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
Suggested Courses

721 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