tags:

views:

45

answers:

1

how can i get a regular expression to match whole words like grand and not match mygrandma.

for example, i wish to replace "grand" with "total",

so "the grand sum is 100" should become "the total sum is 100".

however "my grandma likes apples" should not become "my totalma likes apples".

+4  A: 

Wrap the word in word boundary metacharacters \b. Note that since a backslash is there you'll need to escape it or use a verbatim string:

// Verbatim string
@"\bgrand\b"

// Normal string
"\\bgrand\\b"
BoltClock