我今天的问题是:
如何创建能够找到与模式匹配的所有单词的函数?
例如,我们有单词: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 :(得分:2)
if(match.Success && (match.Length > struct.dlugosc))
匹配的长度永远不会超过结构的长度 - 结构的长度至少是字符串的长度,加上其中的所有其他项。
在match.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
------------------------------------------