mplungjan
mplungjan

Reputation: 178285

Regexp to normalise (pad with zeroes) IP addresses

I have a copy of a log file I want to make easier to view/edit.
I use textpad to remove stuff I do not want and I can enter a regular expression as search term and use \1.\2.\3.\4 in the target field for captured groups.
I would like to change all IP addresses which start each line from

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

to

[0-9]{3}\.[0-9]{3}\.[0-9]{3}\.[0-9]{3}

with padded leading zeros How to do that in one go?

Example input:

10.2.123.4
110.12.23.40
123.123.123.123
1.2.3.4

example output

010.002.123.004
110.012.023.040
123.123.123.123
001.002.003.004

See my own answer for what works

Thanks for your input

Upvotes: 5

Views: 5500

Answers (3)

Grant Peters
Grant Peters

Reputation: 7825

Not quite the full, one liner you want, but it at least brings it down to two lines instead of your current 8.

Following the same formatting you used in your answer:

^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}) -> 00\1.00\2.00\3.00\4

^0*([0-9]{3})\.0*([0-9]{3})\.0*([0-9]{3})\.0*([0-9]{3}) -> \1.\2.\3.\4

The way this works is that:

  1. it pads all of the numbers so that there is at least 3 numbers in each section
  2. it pulls out exactly 3 numbers from each section and ignores any leading '0's remaining

Upvotes: 3

mplungjan
mplungjan

Reputation: 178285

Ok, I decided to do it in more than one go. I post it here for future reference or in case someone comes up with a single regex

Note there is a trailing space on each find and each replace

^([0-9]{1})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}) -> 00\1.\2.\3.\4 
^([0-9]{2})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}) -> 0\1.\2.\3.\4 

^([0-9]{3})\.([0-9]{1})\.([0-9]{1,3})\.([0-9]{1,3}) -> \1.00\2.\3.\4 
^([0-9]{3})\.([0-9]{2})\.([0-9]{1,3})\.([0-9]{1,3}) -> \1.0\2.\3.\4 

^([0-9]{3})\.([0-9]{3})\.([0-9]{1})\.([0-9]{1,3}) -> \1.\2.00\3.\4 
^([0-9]{3})\.([0-9]{3})\.([0-9]{2})\.([0-9]{1,3}) -> \1.\2.0\3.\4 

^([0-9]{3})\.([0-9]{3})\.([0-9]{3})\.([0-9]{1}) -> \1.\2.\3.00\4 
^([0-9]{3})\.([0-9]{3})\.([0-9]{3})\.([0-9]{2}) -> \1.\2.\3.0\4 

Textpad syntax:

^\([0-9]\{1\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\) -> 00\1.\2.\3.\4 
^\([0-9]\{2\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\) -> 0\1.\2.\3.\4 

^\([0-9]\{3\}\)\.\([0-9]\{1\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\) -> \1.00\2.\3.\4 
^\([0-9]\{3\}\)\.\([0-9]\{2\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\) -> \1.0\2.\3.\4 

^\([0-9]\{3\}\)\.\([0-9]\{3\}\)\.\([0-9]\{1\}\)\.\([0-9]\{1,3\}\) -> \1.\2.00\3.\4 
^\([0-9]\{3\}\)\.\([0-9]\{3\}\)\.\([0-9]\{2\}\)\.\([0-9]\{1,3\}\) -> \1.\2.0\3.\4 

^\([0-9]\{3\}\)\.\([0-9]\{3\}\)\.\([0-9]\{3\}\)\.\([0-9]\{1\}\) -> \1.\2.\3.00\4 
^\([0-9]\{3\}\)\.\([0-9]\{3\}\)\.\([0-9]\{3\}\)\.\([0-9]\{2\}\) -> \1.\2.\3.0\4 

Upvotes: 2

Tomalak
Tomalak

Reputation: 338316

Spit on ".", pad, join. No regex needed. Regex would not provide any benefit, even.

JavaScript, for example:

var ip = "110.12.23.40";

ip = ip.split(".").map( function(i) {
  return ("00"+i).slice(-3);
}).join(".");

alert(ip);  // 110.012.023.040

Upvotes: 1

Related Questions