C# Regular Expressions

Introduction to C# Reglar Expressions

Regular expressions या Regex एक text pattern matching tool है। इस tool की सहायता से आप text data के साथ searching, replacing, extracting, modifying और validation आदि operations perform कर सकते है।

Data कई forms में available होता है। जैसे की image, video और text आदि। जब data text की form में होता है तो वह एक pattern follow करता है।

उदहारण के लिए जब किसी website का नाम लिखा जाता है तो निचे दिया गया pattern follow किया जाता है।

www[dot]domain-name[dot]domain-extension

जब कोई date लिखी जाती है तो वह निचे दी गयी pattern में लिखी जाती है।

dd/mm/yyyy

जब कोई email id लिखी जाती है तो वह निचे दिए गए pattern को follow करती है।

email-id[@]email-server[dot]extension

इसी प्रकार सभी प्रकार का text data एक pattern को follow करता है। इन patterns को identify और use करके text data के साथ विभिन्न operations perform किये जा सकते है।

इसके लिए आप regular expression के रूप में वह pattern define करते है जिसके आधार पर आप text data को search, replace, extract, modify या validate करना चाहते है।

उदाहरण के लिए आप किसी बड़े text data में से सिर्फ email id’s को ही extract करके display करना चाहते है तो इसके लिए आप regular expression के रूप में email id का pattern define करते है और उसके बाद उस text data के पुरे text को उस pattern से match करते है। जो text pattern से match होता है वह extract हो जाता है।

इसी प्रकार मान लीजिये आप user द्वारा input के रूप में दी गयी email id को validate करना चाहते है और सुनिश्चित करना चाहते है की user ने सही रूप में email id provide की है तो इसके लिए भी regular expression के रूप में pattern define करके उसे user के input के साथ match कर सकते है और appropriate actions ले सकते है।

C# Regex Class

सभी modern programming languages में regular expression का प्रयोग किया जाता है। C# में regular expression को Regex कहा जाता है। मूल रूप से Regex एक class है जो regular expressions define करने के लिए प्रयोग की जाती है।

यह class कई महत्वपूर्ण methods provide करती है जो regular expression के आधार पर text data के साथ operations perform करने के लिए use किये जाते है। इस class को C# में System.Text.RegularExpressions namespace में define किया गया है।

इस namespace में और भी अन्य classes available है जो अलग अलग regular expressions को represent करती है। किसी program में Regex class को उपयोग करने के लिए आपको इस namespace को include करना आवश्यक होता है।

Creating C# Regular Expressions

C# में regular expression create करने के लिए आप Regex class का object create करते है। इस object को create करते समय ही आप argument के रूप में pattern pass करते है। इसका general syntax निचे दिया जा रहा है।

Regex obj = new Regex(pattern);

Regular expressions में pattern का role महत्वपूर्ण होता है। सही ढंग से operations perform करने के लिए pattern का सही होना आवश्यक है। एक pattern आप निचे दिए जा रहे elements के द्वारा define करते है।

Quantifiers

Quantifiers के द्वारा आप बताते है की किसी character या character group की कितनी occurrences होनी चाहिए। कुछ important quantifiers के बारे में निचे बताया जा रहा है।

QuantifiersExplanations 
*यह quantifier इससे पूर्व define किये गए किसी character को zero या उससे अधिक बार match करता है। 
+यह quantifier इससे पूर्व define किये गए किसी character को 1 या उससे अधिक बार match करता है। 
?यह quantifier इससे पूर्व define किये गए किसी character को 0 या एक बार match करता है। 

Special Characters

Regular expressions को build करने के लिए कुछ special characters का भी प्रयोग किया जाता है। इनके बारे में निचे बताया जा रहा है।

Special CharactersExplanations 
^यह character किसी string की beginning को match करने के लिए प्रयोग किया जाता है। 
$यह character किसी string के end को match करने के लिए प्रयोग किया जाता है। 
. (dot)Dot किसी किसी भी character को सिर्फ एक बार match करता है। 
\dयह किसी digit character को match करने के लिए प्रयोग किया जाता है। 
\Dयह किसी non digit character को match करने के लिए प्रयोग किया जाता है। 
\wयह किसी alphanumeric character और _(underscore) को match करने के लिए प्रयोग किया जाता है। 
\Wयह किसी non word character को match करने के लिए प्रयोग किया जाता है। 
\sयह white space characters को match करने के लिए प्रयोग किया जाता है। 
\Sयह non-white characters को match करने के लिए प्रयोग किया जाता है। 
\nयह new line characters को match करने के लिए प्रयोग किया जाता है।

Grouping

Grouping के द्वारा आप characters के group से regular expression build कर सकते है।

Grouping Explanation 
[]ये brackets एक characters की range को match करने के लिए प्रयोग किया जाता है। इसमें आप range define करते है। 
{}ये brackets इस पहले आने वाले characters को इसमें specify किये गए times के लिए match करता है।  
()ये brackets group expressions के लिए प्रयोग किया जाता है। 

Using C# Regular Expressions

एक बार Regex class का object create करने के बाद आप Regex class में available methods को call कर सकते है। इन methods में string या text को argument के रूप में pass किया जाता है।

C# Regex class में available कुछ methods के बारे में निचे बताया जा रहा है।

  • public bool isMatch(string input, int startPos)
  • public string replace(string input, string replacement-string)
  • public bool isMatch(string input)

Example of C# Regular Expressions

C# में regular expressions के उपयोग को निचे उदाहरण द्वारा समझाया जा रहा है।

using System;
using System.Text.RegularExpressions;

class myClass
{
    static void Main(string[] args)
    {
         string pattern = "[0-9]";
         Regex obj = new Regex(pattern);
         string inputText = "5";

          if(obj.IsMatch(inputText))
          {
                 Console.WriteLine("{0} is a digit between 0 to 9",inputText);
          }
          else
          {
                Console.WriteLine("{0} is not a digit between 0 to 9",inputText);
           }
    }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

5 is a digit between 0 to 9