In reference to my question Regex expression to match whole word with special characters not working ?,
I got an answer which said
@"(?<=^|\s)" + pattern + @"(?=\s|$)"
This works fine for all cases except 1 case. It fails when there is space in the pattern.
Assume string is “Hi this is stackoverflow” and pattern is “this ” , then it says no matches. This happens because of an empty space after the actual string in pattern.
How can we handle this ? Ideally speaking it should say one match found !
Try this
See it here on Regexr
This will also work for pattern that starts with a whitespace.
Basically, what I am doing is to define a custom “word” boundary. But it is not true on a
\W=>\wor a\w=>\Wchange, its true on a\S=>\sor a\s=>\Schange!Here is an example in c#:
Update:
This “Whitespace” boundary can be done more general, so that on each side of the pattern is the same expression, like this
In c#: