在你进行下一步之前,让我们快速了解一下正则表达式的基本术语。
捕获:当你使用一个子表达式匹配一个正则表达式的结果。Capture和CaptureCollection类表示来自单个或一组成功的子表达式捕获的结果。
分组:一个正则表达式可以是一个或多个组组成。Group 类表示来自单个捕获组的结果。GroupCollection类表示一次匹配中捕获的组的集合。
匹配:正则表达式匹配后得到的结果。Match类表示单个正则表达式匹配的结果。MatchCollection 类
表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合。
因此,正则表达式相关的对象之间的关系是:
Regex class --> MatchCollection --> Match objects --> GroupCollection --> Group objects --> CaptureCollection --> Capture objects
| 方法 | 描述 |
| IsMatch | IsMatch() 指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项。如何匹配返回true,否则返回false。 |
| Match | Match() 在该字符串中搜索正则表达式的第一个匹配项。匹配成功返回一个Match对象。 |
| Matches | Matches() 在指定的输入字符串中搜索正则表达式的所有匹配项。 |
| Replace | 使用指定的替换字符串替换与某个正则表达式模式匹配的所有字符串。 |
| Split | 根据正则表达式模式定义拆分输入字符串,它返回一个字符串的数组。 |
using System.Text.RegularExpressions;
static void Main(string[] args)
{
string source = args[0];
string pattern = @"http(s)?://([w-]+.)+[w-]+(/[w-./?%&=]*)?";
bool success = Regex.IsMatch(source, pattern);
if (success)
{
Console.WriteLine("Entered string is a valid URL!");
}
else
{
Console.WriteLine("Entered string is not a valid URL!");
}
Console.ReadLine();
}
Regex ex = new Regex(pattern);
success = ex.IsMatch(source);
static void Main(string[] args)
{
string source = args[0];
string pattern = @"http(s)?://([w-]+.)+[w-]+(/[w-./?%&=]*)?"; Match match = Regex.Match(source, pattern);
if(match.Success)
{
Console.WriteLine("Entered string is a valid URL!");
Console.WriteLine("{0} Groups教程", match.Groups.Count);
for(int i=0;i{
Console.WriteLine("Group {0} Value = {1} Status = {2}",
i, match.Groups[i].Value, match.Groups[i].Success);
Console.WriteLine("t{0} Captures", match.Groups[i].Captures.Count);
for (int j = 0; j {
Console.WriteLine("tt Capture {0} Value = {1} Found at = {2}",
j, match.Groups[i].Captures[j].Value, match.Groups[i].Captures[j].Index);
}
}
}
else
{
Console.WriteLine("Entered string is not a valid URL!");
}
Console.ReadLine();
}

static void Main(string[] args)
{
string source = args[0];
string pattern = @"http(s)?://([w-]+.)+[w-]+(/[w-./?%&=]*)?";
Match match = Regex.Match(source, pattern);
if (match.Success)
{
Console.WriteLine("Entered string is a valid URL!");
Console.WriteLine("{0} Groups", match.Groups.Count);
for (int i = 0; i {
Console.WriteLine("Group {0} Value = {1} Status = {2}",
i, match.Groups[i].Value, match.Groups[i].Success);
Console.WriteLine("t{0} Captures", match.Groups[i].Captures.Count);
for (int j = 0; j {
Console.WriteLine("tt Capture {0} Value = {1} Found at = {2}",
j, match.Groups[i].Captures[j].Value, match.Groups[i].Captures[j].Index);
}
}
}
else
{
Console.WriteLine("Entered string is not a valid URL!");
}
Console.ReadLine();
}
static void Main(string[] args)
{
string source = args[0];
string pattern = @"http(s)?://([w-]+.)+[w-]+(/[w-./?%&=]*)?";
string result = Regex.Replace(source,pattern,"[*** URLs not allowed ***]");
Console.WriteLine(result);
Console.ReadLine();
}
string strDate = "2011-12-31";
string[] dates = Regex.Split(strDate, "-");
foreach (string s in dates)
{
Console.WriteLine(s);
}
Console.ReadLine();
| None | 不可用 | 使用默认行为。 |
| IgnoreCase | i | 使用不区分大小写的匹配。 |
| Multiline | m | 使用多线模式,其中 ^ 和 $ 匹配每行的开头和末尾(不是输入字符串的开头和末尾)。 |
| Singleline | s | Use single-line mode, where the period (.) matches every character (instead of every character except n). |
| ExplicitCapture | n | 不捕获未命名的组。 唯一有效的捕获是显式命名或编号的 ((? |
| Compiled | 不可用 | 将正则表达式编译为程序集。 |
| IgnorePatternWhitespace | x | 从模式中排除保留的空白并启用数字符号 (#) 后的注释。 |
| RightToLeft | 不可用 | 更改搜索方向。 搜索是从右向左而不是从左向右进行。 |
| ECMAScript | 不可用 | 为表达式启用符合 ECMAScript 的行为。 |
| CultureInvariant | 不可用 | 忽略语言的区域性差异。 有关固定区域性的更多信息,请参见 。 |
static void Main(string[] args)
{
string source = args[0];
bool success1 = Regex.IsMatch(source, "hello");
Console.WriteLine("String found? {0}", success1);
bool success2 = Regex.IsMatch(source, "hello", RegexOptions.IgnoreCase);
Console.WriteLine("String found? {0}", success2);
Console.ReadLine();
}

罚罪1+2全80集阿里云盘全集资源链接-罚罪1+2全集高清1080P/4K阿里云盘资源下载无删减
邵氏精品禁品未删减资源在线看-必看的50部邵氏电影无删减观看入口
羞羞漫画高清最新资源-正规平台免费入口与无删减资源导航
日漫入口-官方认证安全入口与无删减日漫资源直达导航
一拳超人漫画入口-2026最新免费高清全集在线看
杨紫《生命树》1080p全40集迅雷磁力链接分享-杨紫《生命树》40集高清完整版迅雷云盘下载1080P(4k)资源