match exact string

losorio

New member
Joined
Jan 5, 2015
Messages
1
Programming Experience
1-3
I?m trying to develop this code that haves a string(ing) and a string array (splitwords).
I need to run a string and if it matches the string from the string array does something with that original string.

C#:
[COLOR=#2B91AF]StringBuilder[/COLOR] builder = [COLOR=#00008B]new[/COLOR] [COLOR=#2B91AF]StringBuilder[/COLOR]();

ing = [COLOR=#800000]" "[/COLOR] + ing + [COLOR=#800000]" "[/COLOR];

builder.[COLOR=#2B91AF]Append[/COLOR]([COLOR=#800000]@"{\rtf1\ansi"[/COLOR]);

[COLOR=#00008B]foreach[/COLOR] ([COLOR=#00008B]string[/COLOR] word [COLOR=#00008B]in[/COLOR] splitwords)
{

[COLOR=#00008B]if[/COLOR] ([COLOR=#2B91AF]Regex[/COLOR].[COLOR=#2B91AF]IsMatch[/COLOR](ing, [COLOR=#800000]@"(?<![\w])"[/COLOR] + word + [COLOR=#800000]@"(?![\w])"[/COLOR]))
{
// for example [COLOR=#808080]put the word in UPPERCASE[/COLOR]
}

builder.[COLOR=#2B91AF]Append[/COLOR](ing);
builder.[COLOR=#2B91AF]Append[/COLOR]([COLOR=#800000]@"}"[/COLOR]);


[COLOR=#00008B]return[/COLOR] builder.[COLOR=#2B91AF]ToString[/COLOR]();

With all the tests it runs ok, but if the word (in the splitwords) to match is "egg" and in the text it has the word "I need to run the string with the word eggs in it" he thinks it s a match. How can I change my if condition to solve this problem?

It returns "I need to run the string with the word EGG s in it"
 
Last edited:
Top Bottom