2010-07-14 13 views
14

क्यों निम्नलिखित कोड परिणाम में:रेगेक्स समूहों के साथ कई घटनाओं को कैसे ढूंढें?

वहाँ ''

के लिए 3 मैचों था:

वहाँ ''

और नहीं के लिए 1 मैचों था

using System; 
using System.Text.RegularExpressions; 

namespace TestRegex82723223 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string text = "C# is the best language there is in the world."; 
      string search = "the"; 
      Match match = Regex.Match(text, search); 
      Console.WriteLine("there was {0} matches for '{1}'", match.Groups.Count, match.Value); 
      Console.ReadLine(); 
     } 
    } 
} 

उत्तर

29
string text = "C# is the best language there is in the world."; 
string search = "the"; 
MatchCollection matches = Regex.Matches(text, search); 
Console.WriteLine("there was {0} matches for '{1}'", matches.Count, search); 
Console.ReadLine(); 
2

Matchपहले मैच लौटाता है, बाकी को कैसे प्राप्त करें के लिए this देखें।

आपको इसके बजाय Matches का उपयोग करना चाहिए। तो फिर तुम इस्तेमाल कर सकते हैं: निर्दिष्ट नियमित अभिव्यक्ति की पहली आवृत्ति के लिए

MatchCollection matches = Regex.Matches(text, search); 
Console.WriteLine("there were {0} matches", matches.Count); 
11

Regex.Match(String, String)

खोजों निर्दिष्ट इनपुट स्ट्रिंग।

उपयोग Regex.Matches(String, String) बजाय।

एक निर्दिष्ट नियमित अभिव्यक्ति की सभी घटनाओं के लिए निर्दिष्ट इनपुट स्ट्रिंग खोजता है।

1

आप Regex.Matches बजाय Regex.Match का उपयोग करना चाहिए अगर आप कई मैचों वापस जाने के लिए चाहते हैं।

संबंधित मुद्दे