vb.net - How to find exact word from sentence -
in window application want find exact word sentence.the 2 field textbox(word) , richtextbox(sentence) respectively.
i've used str(i).contains(word) , each match match in regex.matches(str(i), word).
but these 2 not exact word.for example , word:ate sentence:parfaite ment aux exigences cre*ate* évolutives du marché d aujourd hui
in above fetches ate in create.but need focus onle ate not combined.
in regex.matches, need use \b word boundaries character. wrote example code in c#, , noticed question vb.net, i'll add both code examples:
c#:
//example 1: var teststring = "parfaite ment aux exigences create évolutives du marché d aujourd hui"; var pattern = "ate"; matchcollection found = regex.matches(teststring, @"\b" + pattern + @"\b"); if (found.count > 0) { foreach (match f in found) { console.writeline("'{0}' found @ position {1} in given teststring.", f.value, f.index); } } else console.writeline("no matches in given teststring."); //example 2: var teststring1 = "parfaite ment aux exigences cre*ate* évolutives du marché d aujourd hui don't know language this: ate , last 1 should found: ate!"; var pattern1 = "ate"; matchcollection found1 = regex.matches(teststring1, @"\b" + pattern1 + @"\b"); if (found1.count > 0) { foreach (match f in found1) { console.writeline("'{0}' found @ position {1} in given teststring1.", f.value, f.index); } } else console.writeline("no matches in given teststring1."); console.readline();
vb.net:
'example 1: dim teststring = "parfaite ment aux exigences create évolutives du marché d aujourd hui" dim pattern = "ate" dim found matchcollection = regex.matches(teststring, "\b" & pattern & "\b") if found.count > 0 each f in found console.writeline("'{0}' found @ position {1} in given teststring.", f.value, f.index) next else console.writeline("no matches in given teststring.") end if 'example 2: dim teststring1 = "parfaite ment aux exigences cre*ate* évolutives du marché d aujourd hui don't know language this: ate , last 1 should found: ate!" dim pattern1 = "ate" dim found1 matchcollection = regex.matches(teststring1, "\b" & pattern1 & "\b") if (found1.count > 0) each f match in found1 console.writeline("'{0}' found @ position {1} in given teststring1.", f.value, f.index) next else console.writeline("no matches in given teststring1.") end if console.readline()
Comments
Post a Comment