Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-27
5
Medium Priority
?
554 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 85

Accepted Solution

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

Assisted Solution

by:gdupadhyay
gdupadhyay earned 960 total points
ID: 22815914

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

Assisted Solution

by:TechTiger007
TechTiger007 earned 80 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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 …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

927 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