2013-10-31 4 views
0

다음 문자열에서 특정 텍스트를 검색하고 싶습니다. 문자열에 굵은 태그와 단락 태그가 있습니다. 아래에있는 텍스트 만 검색하고 싶습니다. ...). 이것은 나의 요구 사항입니다. 검색된 값을 문자열 배열에 저장하려고합니다.C에서 주어진 문자열에서 특정 텍스트를 검색하는 방법 #

SampleText<b>Billgates</b><p>This is Para</p><b>SteveJobs</b>ThisisEnd 

다음과 같이 출력해야합니다.

str[0] = Billgates 
str[1] = SteveJobs 

답변

0
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string[] strArray = new string[50]; 
      string str = "SampleText<b>Billgates</b><p>This is Para</p><b>SteveJobs</b>ThisisEnd"; 

      Regex expression = new Regex(@"\<b\>(.*?)\</b\>"); 

      for (int i = 0; i < expression.Matches(str).Count; ++i) 
      { 
       string value = expression.Matches(str)[i].ToString(); 

       value = value.Replace("<b>", ""); 
       value = value.Replace("</b>", ""); 
       strArray[i] = value; 
      } 

      Console.WriteLine(strArray[0]); 
      Console.WriteLine(strArray[1]); 

      Console.ReadLine(); 
     } 
    } 
} 
+0

정말 고마워요. – Arjunan

1

당신은 정규식을 통해 구문 분석을 시도 할 수 :

Regex expression = new Regex(@"\<b\>(.*?)\<b\>"); //This matches anything between <b> and </b> 

foreach (Match match in expression.Matches(code)) //Code being the string that contains '...<b>BillGates</b>...<b>etc</b>...' 
{ 
    string value = match.Groups[1].Value; 
    //And from here do whatever you like with 'value' 
} 
+1

당신은 의미 match.Groups [1] .Value, 나는 생각한다. –

+0

** 편집 : ** 으악, 내가 너에게 잘못한 것 같다, 맞다 네! – AlphaDelta

관련 문제