I need to match a string for something so that it doesn’t accidentally match in a word as well. For example, let us say I was to match the string “de”. I want it to match to these strings:
"de"
" de"
"de "
" de "
" de "
"I like the term de very much"
And things like that, but I don’t want it to match strings like
"dead"
" delaware"
"This is delaware"
And ETC. Can anyone help me with a regular expression like that?
You want to match the word
de. To do this, assert that it begins and ends on word boundaries:I note that your examples consist solely of strings containing just one word (with optional leading and trailing space). But your question did not stipulate that matching strings must contain just one word, so it’s unclear whether this is a requirement or not. The regex above will match strings containing the word
de, whether or not there are also other words in the string. For example, this string will match:This string will not: