Solved

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

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

0
Comment
Question by:P1ST0LPETE
[X]
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
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 240 total points
ID: 22815820
try \. or [.]
0
 
LVL 9

Assisted Solution

by:gdupadhyay
gdupadhyay earned 240 total points
ID: 22815914

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

Assisted Solution

by:TechTiger007
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;
    }

0
 
LVL 10

Author Comment

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

Author Closing Comment

by:P1ST0LPETE
ID: 31510471
Thank you ozo and qdupadhyay.
0

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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