C# Regex。匹配方法
该方法匹配模式的实例,并用于根据模式提取值。
让我们看看如何检查有效的 URL。
为此,请将正则表达式传递给 Matches 方法。
MatchCollection mc = Regex.Matches(text, expr);
上面,expr 是我们设置的表达式,用于检查有效的 URL。
"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?”
我们设置检查的文本是一个 URL,即
https://demo.com
让我们看看完整的代码。
示例
using System;
using System.Text.RegularExpressions;
namespace Demo {
class Program {
private static void showMatch(string text, string expr) {
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
static void Main(string[] args) {
string str = "https://demo.com";
Console.WriteLine("Matching URL...");
showMatch(str, @"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?");
Console.ReadKey();
}
}
}输出
Matching URL... https://demo.com
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP