2011-11-08 4 views
0

제 질문은 오늘입니다 :
패턴과 일치하는 모든 단어를 찾을 수있는 함수를 만들려면 어떻게해야합니까?구조체 목록에서 단어를 검색하고 접미사가있는 단어를 모두 얻으십시오.

예를 들어 우리는 단어가 duck이고, 우리는 그 단어 오리에서 시작하는 모든 단어를 찾고 싶습니다.

나는 성능 최적화 기능을 찾고 있는데 LINQ를 사용할 수 있다면 기쁠 것입니다.

public List<List<string>> FindWordsPostfix(List<Parameters.Words> wordsChess, List<string> wordsFromDictionary, int width) 
    { 
     List<List<string>> listPostfixForstructxIndex = new List<List<string>>(); 

     foreach (Parameters.Words structx in wordsChess) 
     { 
      //1for each structx I have some parameters eg. name, length, index 
      //2for each word (name) I need find word from dict. starting that word(name) 

      //list storing lists of words for each of the structx object 
      List<string> list = new List<string>(); 

      foreach (String wordDictionary in wordsFromDictionary) 
      { 
       Match match = Regex.Match(wordDictionary, structx.word, RegexOptions.IgnoreCase); 
       if(match.Success && (match.Length > structx.length)) 
       { 
        list.Add(match.Value); 
       } 

      } 
      //add list of words to main list 
      listPostfixForstructxIndex.Add(list); 
     } 
     throw new NotImplementedException(); 
    } 

Parameters.Words 포함하는 구조체는 다음과 같습니다 : string name, int length, etc.. 은 지금까지 나는 그런 일 (아직 작동하지 않습니다)를했다.

왜 내 기능이 좋지 않고 데이터를 저장하지 않습니까?

PS2. 나는 그 질문을 편집했다. 나는 그 일을 정리해야했다. 성냥의 길이가 결코 구조체의 길이보다 더 될 것 없다

+0

아마도 _helloing_가 비활성화되었습니다. 하지만 아무런 인사도없이 질문을 작성할 수 있습니다.) – Abel

+0

코드를 읽은 후에도 무엇을 하려는지 확실하지 않습니다. 일련의 글자 또는 패턴으로 끝나는 단어를 찾고 싶지만 목록에 목록이있는 이유를 이해하지 못하는 것 같습니다. – Dracorat

+0

@Dracorat 질문을 편집 할 수 있습니다. 내 영어가 너무 나쁘면 내 질문을 수정해야합니다. 그러나 나는 다시 설명하려고 노력할 것이다 :) 이것은 예이다 : 나는 단어'금지'를 가지고 있고 결과가 될 수 있도록'ban'로 시작하는 모든 단어를 찾고 싶다 : 우리는'List '을 저장하고있다 :'banner''bans'' 은행가 '등으로 목록에 올렸습니다. 목록의 색인은 다른 기능을 필요로합니다. 그럴거야. – deadfish

답변

2
if(match.Success && (match.Length > struct.dlugosc)) 

는 - 최소한 구조체의 길이는 문자열, 플러스의 모든 다른 항목의입니다.

경기 후에 다른 테스트를 해본 적이 있습니다 .Success? 다음 당신은 내가 당신이 요구하는지 생각에 대한 몇 가지 일치하는 코드를 원하는 경우

는 매력을 작동합니다

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

using System.Text.RegularExpressions; 

namespace Word_Ending_Finder 
{ 
    public partial class Form1 : Form 
    { 
     private List<string> WordsToFind = new List<string>(); 
     private List<MySpecialStringStruct> PassagesToSearch = new List<MySpecialStringStruct>(); 

     public Form1() 
     { 
      InitializeComponent(); 
      PassagesToSearch.Add(new MySpecialStringStruct("This is a test passage with a test ending.", 0)); 
      PassagesToSearch.Add(new MySpecialStringStruct("This is a second test passage with a test ending.", 0)); 
      PassagesToSearch.Add(new MySpecialStringStruct("This is a third passage that won't match.", 0)); 

      WordsToFind.Add(@"ing\b"); 
      WordsToFind.Add(@"\bsecond\b"); 
      WordsToFind.Add(@"\bgarbage text\b"); 
     } 

     private void bnGo_Click(object sender, EventArgs e) 
     { 
      txtResults.Text = ""; 
      string Separator = "------------------------------------------"; 

      StringBuilder NewText = new StringBuilder(); 
      foreach (string SearchWord in WordsToFind) 
      { 
       NewText.AppendLine(string.Format("Now searching {0}", SearchWord)); 
       List<MatchValue> Results = FindPassages(PassagesToSearch, SearchWord); 
       if (Results.Count == 0) 
       { 
        NewText.AppendLine("No Matches Found"); 
       } 
       else 
       { 
        foreach (MatchValue ThisMatch in Results) 
        { 
         NewText.AppendLine(string.Format("In passage \"{0}\":", ThisMatch.WhichStringStruct.Passage)); 
         foreach (Match M in ThisMatch.MatchesFound) 
         { 
          NewText.AppendLine(string.Format("\t{0}", M.Captures[0].ToString())); 
         } 
        } 
       } 
       NewText.AppendLine(Separator); 
      } 

      txtResults.Text = NewText.ToString(); 
     } 

     private List<MatchValue> FindPassages(List<MySpecialStringStruct> PassageList, string WhatToFind) 
     { 
      Regex MatchPattern = new Regex(WhatToFind); 
      List<MatchValue> ReturnValue = new List<MatchValue>(); 
      foreach (MySpecialStringStruct SearchTarget in PassageList) 
      { 
       MatchCollection MatchList = MatchPattern.Matches(SearchTarget.Passage); 
       if (MatchList.Count > 0) 
       { 
        MatchValue FoundMatchResult = new MatchValue(); 
        FoundMatchResult.WhichStringStruct = SearchTarget; 
        FoundMatchResult.MatchesFound = MatchList; 
        ReturnValue.Add(FoundMatchResult); 
       } 
      } 
      return ReturnValue; 
     } 
    } 

    public class MatchValue 
    { 
     public MySpecialStringStruct WhichStringStruct; 
     public MatchCollection MatchesFound; 
    } 

    public struct MySpecialStringStruct 
    { 
     public string Passage; 
     public int Id; 

     public MySpecialStringStruct(string passage, int id) 
     { 
      Passage = passage; 
      Id = id; 
     } 
    } 
} 

출력 :

Now searching ing\b 
In passage "This is a test passage with a test ending.": 
ing 
In passage "This is a second test passage with a test ending.": 
ing 
------------------------------------------ 
Now searching \bsecond\b 
In passage "This is a second test passage with a test ending.": 
second 
------------------------------------------ 
Now searching \bgarbage text\b 
No Matches Found 
------------------------------------------ 
+0

struct.dlugosc는 인덱스 x의 구조체에있는 단어의 길이입니다 – deadfish

관련 문제