P1ST0LPETE
asked on
Removing "." (periods) in IP address using regex
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.
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you ozo and qdupadhyay.
ASKER
That didn't make much sense. The code you posted, is the code I'm already using and didn't work.