一:什么是正则表达式
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符以及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑,用来做检索或匹配。简写为regex
二:语法规则
正则表达式做检索判断一般都需要以^开头以$结尾,做替换的话不需要
——^和$:匹配开始和结束
using System;
using System.Text.RegularExpressions;
class MainClass
{
static string str = "hello world";
static void Main(string[] args)
{
string newStr1 = Regex.Replace(str, "^", "ha ");
Console.WriteLine(newStr1); //ha hello world
string newStr2 = Regex.Replace(str, "$", " ha");
Console.WriteLine(newStr2); //hello world ha
}
}
——.:匹配除换行外的任意字符
using System;
using System.Text.RegularExpressions;
class MainClass
{
static string str = "hello world";
static void Main(string[] args)
{
string newStr = Regex.Replace(str, ".", "ha");
Console.WriteLine(newStr); //hahahahahahahahahahaha
}
}
——*:匹配0个或多个字符,等价于{0,}
——?:匹配0个或1个字符,等价于{0,1}
——+:匹配1个或多个字符,等价于{1,}
——\w:匹配字母、数字、下划线、汉字字符
string pattern = @"^\w*$";
——\W:匹配除了字母、数字、下划线、汉字以外的字符
string pattern = @"^\W*$";
——\s:匹配任意空白字符(\n \r \t \f \v)
string pattern = @"^\s*$";
——\S:匹配除了任意空白字符(\n \r \t \f \v)以外的字符
string pattern = @"^\S*$";
——\d:匹配数字字符
string pattern = @"^\d*$";
——\D:匹配除了数字字符以外的字符
string pattern = @"^\D*$";
——[xyz]:匹配中括号中的字符
using System;
using System.Text.RegularExpressions;
class MainClass
{
static string str = "hello world";
static void Main(string[] args)
{
string pattern = @"[l]";
string newStr = Regex.Replace(str, pattern, "*");
Console.WriteLine(newStr); //he**o wor*d
}
}
——[x-y]:匹配x-y之间的字符
using System;
using System.Text.RegularExpressions;
class MainClass
{
static string str = "hello world";
static void Main(string[] args)
{
string pattern = @"[a-d]";
string newStr = Regex.Replace(str, pattern, "*");
Console.WriteLine(newStr); //hello worl*
}
}
——[^xyz]:匹配除了中括号中的字符
using System;
using System.Text.RegularExpressions;
class MainClass
{
static string str = "hello world";
static void Main(string[] args)
{
string pattern = @"[^lh]";
string newStr = Regex.Replace(str, pattern, "*");
Console.WriteLine(newStr); //h*ll*****l*
}
}
——{n}:匹配前面的字符n次
string pattern = @"^\d{5}$";
——{n,}:匹配前面的字符n次或大于n次
string pattern = @"^\d{5,}$";
——{n,m}: 匹配前面的字符n-m次
string pattern = @"^\d{5,10}$";
——x|y:匹配x或y的字符
string pattern = @"^\d|[a-z]$";
——():对正则表达式进行分组
string pattern = @"^(\d|[a-z]){2}$";
三:常用的正则表达式
//只包含数字,并且数字的个数=n
Regex regex = new Regex(@"^\d{n}$");
//只包含数字,并且数字的个数>=n
Regex regex = new Regex(@"^\d{n,}$");
//只包含数字,n<=并且数字的个数<=m
Regex regex = new Regex(@"^\d{n,m}$");
//只包含汉字
Regex regex = new Regex(@"^[\u4e00-\u9fa5]{0,}$");
//只包含英文和数字
Regex regex = new Regex(@"^[A-Za-z0-9]+$");
//长度为3-20的所有字符
Regex regex = new Regex(@"^.{3,20}$");
//只包含数字字母下划线
Regex regex = new Regex(@"^\w+$");
====================
//验证邮箱
Regex regex = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
//验证手机号
Regex regex = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
//验证身份证
Regex regex = new Regex(@"^\d{15}|\d{18}$");
//验证密码(密码必须由字母开头,只能包含数字字母下划线,并且n+1<=密码位数<=m+1)
Regex regex = new Regex(@"^[a-zA-Z]\w{n,m}$");
====================
//匹配空白行
Regex reg = new Regex(@"\n\s*\r");
//匹配每行的首尾空白符
Regex regex = new Regex(@"^\s*|\s*$");
//匹配//注释
Regex regex = new Regex("//.*\n");