एक रेगुलर एक्सप्रेशन एक पैटर्न है जिसे इनपुट टेक्स्ट के विरुद्ध मैच किया जा सकता है। .NET फ्रेमवर्क एक रेगुलर एक्सप्रेशन इंजन प्रदान करता है जो ऐसे मिलान की अनुमति देता है। एक पैटर्न में एक या अधिक कैरेक्टर लिटरल्स, ऑपरेटर्स, या कंस्ट्रक्ट्स होते हैं।
बेसिक RegEx मेटाकैरेक्टर्स
* = शून्य या अधिक
? = शून्य या एक
^ = नॉट (निगेशन)
[] = रेंज (उदाहरण: 0-9, a-z, A-Z)
विधि 1: Char.IsDigit() का उपयोग करना
using System;
namespace DemoApplication
{
public class Program
{
static void Main(string[] args)
{
string str1 = "123string456";
string str2 = string.Empty;
int val = 0;
Console.WriteLine($"String with number: {str1}");
for (int i = 0; i < str1.Length; i++)
{
if (Char.IsDigit(str1[i]))
str2 += str1[i];
}
if (str2.Length > 0)
val = int.Parse(str2);
Console.WriteLine($"Extracted Number: {val}");
Console.ReadLine();
}
}
}
आउटपुट
String with number: 123string456 Extracted Number: 123456
नोट: यह विधि स्ट्रिंग में सभी अंकों को जोड़ देती है (123 + 456 = 123456)।
विधि 2: Regex का उपयोग करना
using System;
using System.Text.RegularExpressions;
namespace DemoApplication
{
public class Program
{
static void Main(string[] args)
{
string str1 = "123string456";
string str2 = string.Empty;
int val = 0;
Console.WriteLine($"String with number: {str1}");
// \d+ एक या अधिक अंकों से मैच करता है
var matches = Regex.Matches(str1, @"\d+");
foreach (var match in matches)
{
str2 += match;
}
val = int.Parse(str2);
Console.WriteLine($"Extracted Number: {val}");
Console.ReadLine();
}
}
}
आउटपुट
String with number: 123string456 Extracted Number: 123456
एडवांस्ड: अलग-अलग नंबर निकालना (संयुक्त नहीं)
using System;
using System.Text.RegularExpressions;
using System.Linq;
string input = "Order 123 placed on 2023-12-25 for $45.99";
// सभी नंबर ग्रुप्स ढूंढें
var numbers = Regex.Matches(input, @"\d+")
.Cast()
.Select(m => m.Value)
.ToList();
Console.WriteLine("सभी नंबर ग्रुप्स:");
numbers.ForEach(n => Console.WriteLine(n));
// आउटपुट:
// 123
// 2023
// 12
// 25
// 45
// 99
// पहले नंबर के रूप में पार्स करें
int firstNumber = int.Parse(numbers.First());
// या सभी को इंट में पार्स करें
var allNumbers = numbers.Select(int.Parse).ToList();
एडवांस्ड: दशमलव/नेगेटिव नंबर्स के लिए पैटर्न्स
string input = "Temperature: -12.5°C, Price: $99.99, Count: 42";
// इंटीजर्स (नेगेटिव सहित)
var integers = Regex.Matches(input, @"-?\d+")
.Cast().Select(m => int.Parse(m.Value)).ToList();
// Result: -12, 99, 42
// डेसीमल नंबर्स
var decimals = Regex.Matches(input, @"-?\d+\.\d+")
.Cast().Select(m => decimal.Parse(m.Value)).ToList();
// Result: -12.5, 99.99
// करेंसी फॉर्मेट
var prices = Regex.Matches(input, @"\$\d+(\.\d{2})?")
.Cast().Select(m => decimal.Parse(m.Value.Substring(1))).ToList();
// Result: 99.99
रीयूजेबल हेल्पर मेथड्स
public static class StringNumberExtractor
{
/// स्ट्रिंग से पहला इंटीजर निकालता है
public static int? ExtractFirstInt(this string input)
{
if (string.IsNullOrEmpty(input)) return null;
var match = Regex.Match(input, @"-?\d+");
return match.Success ? int.Parse(match.Value) : (int?)null;
}
/// स्ट्रिंग से सभी इंटीजर्स निकालता है
public static List ExtractAllInts(this string input)
{
if (string.IsNullOrEmpty(input)) return new List();
return Regex.Matches(input, @"-?\d+")
.Cast()
.Select(m => int.Parse(m.Value))
.ToList();
}
/// स्ट्रिंग से पहला डेसीमल निकालता है
public static decimal? ExtractFirstDecimal(this string input)
{
if (string.IsNullOrEmpty(input)) return null;
var match = Regex.Match(input, @"-?\d+(\.\d+)?");
return match.Success ? decimal.Parse(match.Value) : (decimal?)null;
}
/// स्ट्रिंग में केवल अंक रखता है (नॉन-डिजिट हटाता है)
public static string KeepOnlyDigits(this string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
return Regex.Replace(input, @"\D", "");
}
}
// उपयोग
"abc123def456".ExtractFirstInt() // 123
"abc123def456".ExtractAllInts() // [123, 456]
"Price: $99.99".ExtractFirstDecimal() // 99.99
"Phone: 123-456-7890".KeepOnlyDigits() // "1234567890"
प्रदर्शन तुलना
| विधि | उपयोग मामला | प्रदर्शन |
|---|---|---|
Char.IsDigit लूप | सिंपल डिजिट फिल्टरिंग | तेज़ (कोई RegEx ओवरहेड नहीं) |
Regex.Matches | पैटर्न-आधारित निष्कर्षण | मध्यम (कंपाइल्ड RegEx के साथ बेहतर) |
Span + मैन्युअल पार्सिंग | उच्च प्रदर्शन (.NET Core 2.1+) | सबसे तेज़ |
कंपाइल्ड RegEx के साथ उच्च प्रदर्शन
public static class NumberPatterns
{
// कंपाइल्ड RegEx - पहली बार धीमा, बाद में तेज़
public static readonly Regex DigitsOnly = new Regex(@"\d+", RegexOptions.Compiled);
public static readonly Regex Integers = new Regex(@"-?\d+", RegexOptions.Compiled);
public static readonly Regex Decimals = new Regex(@"-?\d+\.\d+", RegexOptions.Compiled);
}
public static List ExtractNumbersFast(string input)
{
return NumberPatterns.Integers.Matches(input)
.Cast()
.Select(m => int.Parse(m.Value))
.ToList();
}
सिफारिश: सरल "केवल अंक रखें" के लिए Char.IsDigit या Regex.Replace का उपयोग करें। पैटर्न-आधारित निष्कर्षण (नेगेटिव, डेसीमल, विशिष्ट फॉर्मेट्स) के लिए RegEx का उपयोग करें।