Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now