正则表达式的使用,验证邮箱,IP,和替换字符串
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
namespace w000窗体时间格式
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//替换正则表达式的使用,替换字母a-z以及A-Z的字符,也可以使用IsMatch来验证
string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"[A-Za-z]\*?", textBox2.Text);
MessageBox.Show("替换前" + textBox1.Text + "\n替换的" + textBox2.Text+"\n替换后"+ strResult);
//MessageBox.Show("是否是邮箱" + Ismail(textBox3.Text));
MessageBox.Show("是否是ip" + Ismail(textBox3.Text));
}
public bool Ismail(string strEmail)
{
// return System.Text.RegularExpressions.Regex.IsMatch(strEmail, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z");
return System.Text.RegularExpressions.Regex.IsMatch(strEmail,"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
}
}
}