Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using this code in order to extract sentences, I input word in TextBox1 and if large amount of text in tagetlistBox containing sentences with that word it appears in targetTextBox2. but so far i am generating only first sentence containing specific word. how can i do it for all the text. and extract all the sentences that have that specific word

C#
var regex = new Regex(textBox1.Text);
var text = targetListBox.Text;

var texts = text.Split('.');
foreach (var t in texts)
{
   if (regex.IsMatch(t))
   {
      targetTextBox2.Text = t.Trim();
   }
}
Posted
Updated 20-May-14 23:16pm
v3
Comments
Emre Ataseven 2-Jun-14 12:50pm    
why are u using regex? You can just check it with text.Contains

1 solution

C#
var regex = new Regex(textBox1.Text);
List<string> matchingLines = new List<string>();
foreach (var listBoxItem in targetListBox.Items)// loop every item in your targetListBox
{
    var texts = listBoxItem.Text.Split('.');
    foreach (var t in texts)
    {
     if (regex.IsMatch(t))
     {
         matchingLines.Add(listBoxItem.Text);
         break;
     }
    }
}
targetTextBox2.Lines =matchingLines.ToArray();
 
Share this answer
 
v4
Comments
Asad_Iqbal 2-Jun-14 3:31am    
i tried it for a while but targetListbox.Items isn't supported and i got this as an output
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:
System.Windows.Forms.RichTextBox, Text:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900