在你进行下一步之前,让我们快速了解一下正则表达式的基本术语。
捕获:当你使用一个子表达式匹配一个正则表达式的结果。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 < match.Groups[i].Captures.Count; 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 < match.Groups.Count; 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 < match.Groups[i].Captures.Count; 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();
}
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级