using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespaceUnity
{classProgram
{staticvoid Main(string[] args)
{
string s = "I am blue cat.";
string res1 = Regex.Replace(s, "^", "开始:");//搜索字符串,符合正则表达式的情况。然后把所以符合的位置替换成后面的字符串"^"(定位开始正则表达式)string res2 = Regex.Replace(s, "$", "开始:");//搜索字符串,符合正则表达式的情况。然后把所以符合的位置替换成后面的字符串"^"(定位结尾正则表达式)
Console.WriteLine(res1);
Console.WriteLine(res2);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespaceUnity
{classProgram
{staticvoid Main(string[] args)
{
string s = Console.ReadLine();
string pattern = @"^\d*$";//正则表达式(全部由数字组成),\d代表数字bool isMatch = Regex.IsMatch(s,pattern);//判读是否符合正则表达式
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespaceUnity
{classProgram
{staticvoid Main(string[] args)
{
string s = Console.ReadLine();
string pattern = @"^\w*$";//匹配包括下划线的任何单词字符,\w代表包括下划线的任何单词字符bool isMatch = Regex.IsMatch(s,pattern);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespaceUnity
{classProgram
{staticvoid Main(string[] args)
{
string str = "I am a cat";
string pattern = @"[^ahou]";//匹配除了括号里的其它字符,例如这个例子结果为**a**a**a*string s = Regex.Replace(str, pattern, "*");
Console.WriteLine(s);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespaceUnity
{classProgram
{staticvoid Main(string[] args)
{
string qq1 = "231564";
string qq2 = "121321245654879";
string qq3 = "d1231456";
string pattern = @"^\d{5,12}$";//{n,m}重复字符的次数n到m之间,这个正则表达式可以用来匹配5到12位的数字
Console.WriteLine(Regex.IsMatch(qq1,pattern));
Console.WriteLine(Regex.IsMatch(qq2, pattern));
Console.WriteLine(Regex.IsMatch(qq3, pattern));
}
}
}