Regexp for extracting public IP address

bigdatamarkRecently I needed to make a regular expression for parsing the first public IPV4 address from a string of comma separated private and public IP addresses.

After spending an hour on it and failing, I thought about it over night and woke up with some more ideas of what to try.

I ended up using a word boundary as an anchor and a negative lookahead from the anchor to accomplish it.

Test string:




2 Replies to “Regexp for extracting public IP address”

  1. I found the last 3 octets to be a bit permissive – modified it in my case as it identified invalid octets (serial numbers, versions, etc.) in my data sets:


Leave a Reply

Your email address will not be published. Required fields are marked *