正则表达式:专门用于字符串处理的语言。
可以 解决:
1.检索:获取我们想要的部分
2.匹配:判断给定字符串是否符合正则表达式的过滤逻辑。即表述了字符串书写的规则。
定位元字符 $、^
(要用using System.Text.RegularExpressions;)
$在结尾处插入
^在开头处插入
string s = "Hello World";
string res = Regex.Replace(s, "^", "start");
string res1 = Regex.Replace(s, "$", "end");
基本语法元字符
[ab] 表示字符串允许包含ab字符
[a-d] 表示字符串允许包含abcd内的字符都可以
[^x] 表示字符串不包含x的字符
[^abcd] 表示字符串不包含abcd的字符
\w 表示字符串允许包含大小写字母、数字、下划线
\W 表示字符串不包含大小写字母、数字、下划线
\d 表示字符串只允许包含数字
判断字符串是否仅包含数字:
string pattern = @"^\d*$"
bool IsMatch = Regex.IsMatch(s, pattern);
结果为true,则符合,结果为false则不符合。
同理
string pattern1 = @"^a*$";
表示字符串内有一个或多个a
string pattern2 = @"^\w*$";
/表示字符串包含大小写字母、数字、下划线 \w
class Program
{
static void Main