kenchan0752

(转)C#判断IP地址是否合法正则表达式 .

C#判断IP地址是否合法函数-使用正则表达式
var reg=/(\d{3})\.(\d{3})\.(\d{1,3})\.(\d{3})/  (常用方法)
//------方法一
public   bool   IsCorrenctIP(string ip)
  {  
   string pattrn=@"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])";
   if(System.Text.RegularExpressions.Regex.IsMatch(ip,pattrn)) 
   {
    return   true;  
   }
   else
   {
    return   false;  
  
   }
  }

//------------方法二
  public   bool   IsValidIP(string   ip)  
  {  
   if   (System.Text.RegularExpressions.Regex.IsMatch(ip,"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))  
   {  
    string[]   ips   =   ip.Split(\'.\');  
    if   (ips.Length   ==   4   ||   ips.Length   ==   6)  
    {  
     if(System.Int32.Parse(ips[0])   <   256   &&   System.Int32.Parse(ips[1])   <   256   &   System.Int32.Parse(ips[2])   <   256   &   System.Int32.Parse(ips[3])   <   256)  
      return   true;  
     else  
      return   false;  
    }  
    else  
     return   false; 
   
   }  
   else  
    return   false;  
  }

 

//-----------------------方法三------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CheckIP
{
   
publicpartialclass Form1 : Form
    {
       
publicbool IsIP(string ip)
        {
           
bool b =true;
           
string[] lines =newstring[4];
           
string s =".";
            lines
= ip.Split(s.ToCharArray(), 4);//分隔字符串
            for (int i =0; i <4; i++)
            {
               
if (Convert.ToInt32(lines[i]) >=255|| Convert.ToInt32(lines[i]) <0)
                {
                    b
=false;
                   
return b;
                }
            }
           
return b;
        }
       
publicbool isNu(string a)
        {

        }

       
public Form1()
        {
            InitializeComponent();
        }

       
privatevoid btn_Check_Click(object sender, EventArgs e)
        {
           
string x;
            x
= txt_IP.Text;
           
if (IsIP(x))
                MessageBox.Show(
"你输入的是合法IP!");
           
else
                MessageBox.Show(
"不是合法IP");
        }
    }
}


//-----------------------------------------------------------

Regex r = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
string s = "192.168.1.1";
if (r.IsMatch(s))
{

}

 

//---------------------------------------------------------------------

try...

C# code
if (Regex.IsMatch(yourStr, @"^(?:(?:[1-9]?[0-9]|1[0-9]{2}|2(?:[0-4][0-9]|5[0-5]))\.){3}(?:[1-9]?[0-9]|1[0-9]{2}|2(?:[0-4][0-9]|5[0-5]))$")) richTextBox2.Text ="合法IP"; else richTextBox2.Text ="非法IP";

 

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-12-26
  • 2021-12-26
  • 2021-12-26
  • 2021-11-21
  • 2021-06-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案