I want the same functionality as \\b
but with other characters.
In C#, I want to have something like
string str = \"\\\\b\" + Regex.Escape(
The problem you experience is related to the fact that \b word boundary is context dependent, and \b\(abc\b
will match (abc
in x(abc)
but not in :(abc)
(\b\(
means there must be a word char before (
).
To match any string that is not enclosed with word chars use
var pattern = $@"(?
See the regex demo.
Here, (? is a negative lookbehind that will make sure there is no word char immediately to the left of the current location, and
(?!\w)
negative lookahead will make sure there is no word char immediately to the right of the current location.
Other custom "word" boundaries:
var pattern = $@"(?
c
in c++
): var pattern = $@"(?