Link to home
Start Free TrialLog in
Avatar of P1ST0LPETE
P1ST0LPETEFlag for United States of America

asked on

Removing "." (periods) in IP address using regex

Hi,
Having trouble with the following code.  Not sure why it's not working.  I'm trying to remove the periods (.) from IP addresses, so that I just have a 12 digit number.  For example the IP address of 123.456.789.101 should be converted into 123456879101.  I know the problem is in my regex statement.  If I comment the regex line out, and simply use the  "string ipNumber = ip;" then I get the IP address printed out as expected.  However, if I use the regex line, then the output is blank or null.
public partial class _Default : System.Web.UI.Page
{
    string userIP = HttpContext.Current.Request.UserHostAddress;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        lbAddress.Text = "IP-Address: " + hostAddress + " IP-Number: " + IPAddressConverter(userIP);
 
    }
 
    protected string IPAddressConverter(string ip)
    {
        string ipNumber = Regex.Replace(ip, ".", "");
        //string ipNumber = ip;
        return ipNumber;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of P1ST0LPETE

ASKER

TechTiger007:
That didn't make much sense.  The code you posted, is the code I'm already using and didn't work.
Thank you ozo and qdupadhyay.