locked
Remove leading zeros from IP Address using regular expression RRS feed

  • Question

  • Hi,

    I have an IP like this "127.000.000.001" how can I remove the leading zeros to get this "127.0.0.1"?

    public static string AsIPAddress(this string str)
    {
                str = str == null ? "" : str;
                string pattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";            
                return new Regex(pattern).Replace(str, "$1.$2.$3.$4");
            

    Thanks,

    Thursday, March 29, 2012 4:07 PM

Answers

  • (?<=^|\.)0+(?!\.|$)

    That will match all of the leading zeros.

    • Marked as answer by tBStar Friday, March 30, 2012 2:35 AM
    Thursday, March 29, 2012 4:22 PM
  • Hello, you can use:

    System.Text.RegularExpressions.Regex.Replace("127.000.000.001", "0*([0-9]+)", "${1}")

    Hope this helps,

    Miguel Fernández.

    • Proposed as answer by Norkk Thursday, March 29, 2012 5:03 PM
    • Marked as answer by tBStar Friday, March 30, 2012 2:35 AM
    Thursday, March 29, 2012 4:57 PM
  • Hello,

    You really don't need regex to do that, you can do something like this.

    IPAddress IP = IPAddress.Parse("127.000.000.001");

    IP.ToString()


    Eyal (http://shilony.net), Regards.

    • Marked as answer by tBStar Friday, March 30, 2012 2:35 AM
    Thursday, March 29, 2012 5:54 PM

All replies

  • (?<=^|\.)0+(?!\.|$)

    That will match all of the leading zeros.

    • Marked as answer by tBStar Friday, March 30, 2012 2:35 AM
    Thursday, March 29, 2012 4:22 PM
  • Hi,

    Where do I put it?

    Thanks

    Thursday, March 29, 2012 4:49 PM
  • Hello, you can use:

    System.Text.RegularExpressions.Regex.Replace("127.000.000.001", "0*([0-9]+)", "${1}")

    Hope this helps,

    Miguel Fernández.

    • Proposed as answer by Norkk Thursday, March 29, 2012 5:03 PM
    • Marked as answer by tBStar Friday, March 30, 2012 2:35 AM
    Thursday, March 29, 2012 4:57 PM
  • Hi,

    Where do I put it?

    Thanks

    Replace that pattern with an empty string, and all of the leading zeros will go away.
    Thursday, March 29, 2012 5:15 PM
  • Hello,

    You really don't need regex to do that, you can do something like this.

    IPAddress IP = IPAddress.Parse("127.000.000.001");

    IP.ToString()


    Eyal (http://shilony.net), Regards.

    • Marked as answer by tBStar Friday, March 30, 2012 2:35 AM
    Thursday, March 29, 2012 5:54 PM
  • Hello,

    You really don't need regex to do that, you can do something like this.

    IPAddress IP = IPAddress.Parse("127.000.000.001");

    IP.ToString()


    Eyal (http://shilony.net), Regards.

    This code will interpret the 127.000.000.001 as octal data which will create an unintended value for the IP Address. I recommend using a RegEx solution as the leading zero causes the value to be interpreted differently than you intend. 
    • Proposed as answer by Perter Tuesday, February 25, 2014 8:33 PM
    Friday, January 25, 2013 6:44 PM
  • This code will interpret the 127.000.000.001 as octal data which will create an unintended value for the IP Address. I recommend using a RegEx solution as the leading zero causes the value to be interpreted differently than you intend. 
    Are you for real?

    Regards,

    Eyal Shilony

    Friday, January 25, 2013 7:38 PM
  • PeacError is correct.  You should use regex

    The IP Address object will take leading zeros as octal rather than decimal.  For example:

    192.168.1.64 = 192.168.1.64

    192.168.001.064 = 192.168.1.52!!!

    Note that an IPAddress could also be specified with hex values, but the decimal/octal confusion is more likely to happen.  Some addresses will be the same in decimal and octal but some won't.

    Depending how you are using the IPAddress object be careful of this.

    Tuesday, February 25, 2014 8:33 PM