Solved

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

Posted on 2008-10-27
5
547 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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