यह विधि पैटर्न के उदाहरणों से मेल खाती है और पैटर्न के आधार पर मान निकालने के लिए उपयोग की जाती है।
उदाहरण
आइए देखें कि वैध 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("URL मिलान...");
showMatch(str, @"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?");
Console.ReadKey();
}
}
}
आउटपुट
URL मिलान... https://demo.com