用多个单词搜索星座。 [英] searching sentances with multiple words.
问题描述
var regex = new 正则表达式(textBox1.Text);
List< string> matchingLines = new List< string>();
foreach ( var listBoxItem in targetListBox.ToString()) // 循环targetListBox中的每一项
{
var texts = targetListBox.Text.Split(' 。',' ?',' \ n');
foreach ( var t in text)
{
if (regex.IsMatch(t))
{
// matchingLines.Add(targetTextBox2.Text.ToString());
matchingLines 。新增(t.Trim());
// break;
}
}
break ;
}
targetTextBox2.Lines = matchingLines.ToArray();
我有这个代码来挑选句子文本框中单个单词的基础,但是当它涉及文本框中的多个单词时,它不起作用。任何建议朋友????
这并不会让我感到惊讶:也许你使用了foreach循环变量,它可能会更好一点吗? br />
foreach ( var listBoxItem targetListBox.ToString()) // 循环targetListBox中的每一项
{
var texts = targetListBox.Text.Split(' 。',' ?', ' \ n');
但由于foreach将处理一个字符串,listBoxItem将是一个字符...
和<循环底部的code> break 可能也没有多大帮助......
也许是因为过去尝试过的一些负面经验为了修改或重新设计包含复杂RegEx的代码,我更喜欢在全部代码中执行操作。
我想定义我可以使用的通用例程再次作为实用方法;像这样:// 将文本转换为单词列表
// 需要Linq
/ / string =>列表< string>
私人列表< string> TextToLinesOrWords( bool toWords, bool toDistinct, string theText)
{
// 返回所有单词
< span class =code-keyword> if (toWords)
{
var resultWords = theText.Split(
new char [] {' < span class =code-string>。',' ,',' - ',' ?',' \t',' \ r',' \ n',' !',' ',' ;',' :'},
StringSplitOptions.RemoveEmptyEntries );
if (toDistinct) return resultWords.Distinct()。ToList() ;
return resultWords.ToList();
}
// 返回行
< span class =code-keyword> var resultLines = theText.Split( new char [] {' \ r',' \ n'},StringSplitOptions.RemoveEmptyEntries);
if (toDistinct) return resultLines.Distinct()。ToList() ;
return resultLines.ToList();
}假设ListBox中的项目没有经常更改,我会创建(一次!)每个ListBoxItem(Type Object)中唯一单词的主列表,如下所示: pre lang =c#> private void Test1()
{
< span class =code-keyword> var wordsByListBoxItems =
listBox1.Items.Cast< string>()
.Select(itm = > ; TextToLinesOrWords( true , true ,itm))。ToList();
// 运行时TextBox内容的模拟数据
string test = 两三个字跨度>;
// 将TextBox内容转换为唯一字词列表
var testToWords = TextToLinesOrWords( true , true ,test);
// 构建一个IEnumerable行,其中TextBox中的任何单词
// 匹配任何ListBoxItems中的单词
var matchingLines = wordsByListBoxItems
.Where(line = > line.Intersect(testToWords).Count()> 0 );
// 仅供测试:验证匹配:
< span class =code-keyword> foreach ( var line in matchingLines)
{
Console.WriteLine( 匹配:{0},line);
}
}
var regex = new Regex(textBox1.Text);
List<string> matchingLines = new List<string>();
foreach (var listBoxItem in targetListBox.ToString())// loop every item in your targetListBox
{
var texts = targetListBox.Text.Split('۔', '؟', '\n');
foreach (var t in texts)
{
if (regex.IsMatch(t))
{
//matchingLines.Add(targetTextBox2.Text.ToString());
matchingLines.Add(t.Trim());
//break;
}
}
break;
}
targetTextBox2.Lines = matchingLines.ToArray();
I Have this code to pick sentences on bases of single word in textbox, but when it comes to more then one word in a textbox it doesn't work. any suggestion friends????
That doesn't surprise me too much: Perhaps if you used the foreach loop variable, it might work a little better?
foreach (var listBoxItem in targetListBox.ToString())// loop every item in your targetListBox { var texts = targetListBox.Text.Split('۔', '؟', '\n');But since the foreach will be working on a string, listBoxItem will be a single character...
And thebreak
at the bottom of the loop probably doesn't help much either...
Perhaps because of some negative experiences in the past trying to modify, or re-purpose, code containing complex RegEx's, I prefer to do things in full-on code.
I like to define general purpose routines I can use again as utility methods; like this one:// Convert text into a list of words // requires Linq // string => List<string> private List<string> TextToLinesOrWords(bool toWords, bool toDistinct, string theText) { // return all words if (toWords) { var resultWords = theText.Split( new char[] {'.', ',', '-', '?', '\t', '\r', '\n', '!', ' ', ';', ':'}, StringSplitOptions.RemoveEmptyEntries); if(toDistinct) return resultWords.Distinct().ToList(); return resultWords.ToList(); } // return lines var resultLines = theText.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); if (toDistinct) return resultLines.Distinct().ToList(); return resultLines.ToList(); }Assuming the items in your ListBox didn't change frequently, I'd create (once !) a master list of the unique words in each ListBoxItem (Type Object) like this:
private void Test1() { var wordsByListBoxItems = listBox1.Items.Cast<string>() .Select(itm => TextToLinesOrWords(true, true, itm)).ToList(); // simulated data for TextBox content at run-time string test = "two or three words"; // convert the TextBox content to a list of unique words var testToWords = TextToLinesOrWords(true, true, test); // build an IEnumerable of lines where any word in the TextBox // matches a word in any of the ListBoxItems var matchingLines = wordsByListBoxItems .Where(line => line.Intersect(testToWords).Count() > 0); // for testing only: verify matches: foreach (var line in matchingLines) { Console.WriteLine("matches: {0}", line); } }
这篇关于用多个单词搜索星座。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!