Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this list of words in TextBox1

اردو

ملتان

ظفر

جہانگیر

حامد

جنگ

میر

جیو

and i am trying to pick sentences from richtextbox containing those words. need some help friends.
C#
string[] s = textBox1.Text.ToString().Split(' ');
List words = new List {s.ToString() };
string pattern = string.Join("|", words.Select(w => Regex.Escape(w)));
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
 
foreach (var richtextBoxItem in targetTextBox.ToString())// loop every item in your targetListBox
{
string[] texts = targettextBox.Text.Split(new string[] { "۔", "؟", "\n" }, StringSplitOptions.None);
foreach (var t in texts)
{
 
if (regex.IsMatch(t))
{
//matchingLines.Add(targetTextBox2.Text.ToString());
words.Add(t.Trim());
//break;
}

}
break;
}
targetTextBox2.Lines = words.ToArray();

have use this code to generate lines on basis of first word above, but not working list of words.
Posted
Updated 19-Feb-15 4:29am
v3
Comments
Richard MacCutchan 19-Feb-15 4:22am    
What help exactly? you can use one of the many methods of the string class to find words within strings.
Asad_Iqbal 19-Feb-15 4:26am    
string[] s = textBox1.Text.ToString().Split(' ');
List<string> words = new List<string> {s.ToString() };
string pattern = string.Join("|", words.Select(w => Regex.Escape(w)));
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);

foreach (var listBoxItem in targetListBox.ToString())// loop every item in your targetListBox
{
string[] texts = targetListBox.Text.Split(new string[] { "۔", "؟", "\n" }, StringSplitOptions.None);
foreach (var t in texts)
{

if (regex.IsMatch(t))
{
//matchingLines.Add(targetTextBox2.Text.ToString());
words.Add(t.Trim());
//break;
}

}
break;
}
targetTextBox2.Lines = words.ToArray();

have use this code to generate lines on basis of first word above, but not working list of words.
BillWoodruff 19-Feb-15 9:10am    
Your problem description is inaccurate. You say you're using a TextBox; your code says your are using a ListBox. Get the problem description right, first.
Asad_Iqbal 19-Feb-15 10:30am    
i made a correction friend.. it's two richtextbox and textbox

The first thing to note is that your split operation doesn't split words, as you don't use any inter-word gap character in your split operation.
Try adding space to the Split array and see if that improves things.

Then use the debugger to see exactly what you are checking.

Personally, I wouldn't use a regex for this - I'd use String.ToLower on the input, and then use Enumerable.Contains[^] to see if this word is in the list.
 
Share this answer
 
Comments
Asad_Iqbal 19-Feb-15 5:24am    
string[] s = textBox1.Text.ToString().Split(' ');
@Original Griff if you are pointing towards this then i have entered space here but it doesn't work.
OriginalGriff 19-Feb-15 5:45am    
Not just a space: at teh moment you are splitting on three characters:
"۔", "؟", "\n"
One of which is an end of line.
You need to include characters in your split that actually separate one word from another...
Asad_Iqbal 19-Feb-15 7:55am    
actually i use those three end of line characters to split sentences on the basis of words in textbox1. this is where i am stuck as words in textbox1 are in above form so i am trying to pick sentences from richtextbox1 to richtextbox2 on the basis of words in textbox1. if there is onle single word in textbox1 with no extra spaces then it works fine but in case of more then a single work it doesn't work....
Well you can use regular expression for your purpose. Attach is similar example which searches a given word in a sentence. You may need to do a bit tweaking to get your desired result.

C#: Parse a Sentence Containing a Word from Text using Regular Expressions.[^]
 
Share this answer
 
Comments
Asad_Iqbal 19-Feb-15 5:30am    
its very helpful but my input text is unicode based how can i resolve this?

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