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

Posted on 2008-10-27
Last Modified: 2013-12-17
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

Question by:P1ST0LPETE
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
LVL 84

Accepted Solution

ozo earned 240 total points
ID: 22815820
try \. or [.]

Assisted Solution

gdupadhyay earned 240 total points
ID: 22815914

string ipNumber  = Regex.Replace(ip, "[.]", "");
I will work.
LVL 13

Assisted Solution

TechTiger007 earned 20 total points
ID: 22815978
why dont you try this

protected string IPAddressConverter(string ip)
        string ipNumber = ip.Replace(".", "");
        //string ipNumber = ip;
        return ipNumber;

LVL 10

Author Comment

ID: 22825112
That didn't make much sense.  The code you posted, is the code I'm already using and didn't work.
LVL 10

Author Closing Comment

ID: 31510471
Thank you ozo and qdupadhyay.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 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