Regex से शब्दों का पहला अक्षर निकालना
स्ट्रिंग सेट करें:
string str = "The Shape of Water got an Oscar Award!";
रेगुलर एक्सप्रेशन @"\b[a-zA-Z]" का उपयोग करें:
\b- वर्ड बाउंड्री (शब्द की सीमा)[a-zA-Z]- कोई भी अल्फाबेटिकल कैरेक्टर
पूर्ण कोड उदाहरण
using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
public class Program {
private static void showMatch(string text, string expr) {
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
public static void Main(string[] args) {
string str = "The Shape of Water got an Oscar Award!";
Console.WriteLine("Display first letter of each word!");
showMatch(str, @"\b[a-zA-Z]");
}
}
}
आउटपुट
Display first letter of each word! The Expression: \b[a-zA-Z] T S o W g a O A
नोट: "of" का 'o' और "an" का 'a' भी मिलते हैं क्योंकि ये भी शब्द हैं। यदि केवल महत्वपूर्ण शब्द चाहिए, तो स्टॉप वर्ड्स फिल्टर करें।