I want the regex in tcl to match my specified word boundary, without any special characters such as +-.() in front or back.
Here are the things I tried and it just doesn't match properly:
Let's say I have the following string:
hello world +hello world -hello world hello+ hello
I want it to only match hello, not hello+ or -hello.
\bhello\b
hello+hello-hellohello+
[^+-]\bhello\b[^+-]
- no matches
[^+-]\bhello\b
- (doesn't match the first
helloeven though it should've matched) hello+hello
(?![+-])\bhello\b(?![+-])
hello+hello-hello
As documented, Tcl uses
\yto match a word boundary, not\b(which is a backspace character for compatibility with the escapes used by general Tcl code). This means you need an RE something like this:The middle piece is
\yhello\ywhich matches the word, and then we need^|[^-+]at the beginning to match either the beginning of the string or a character other than-or+, and equivalently$|[^-+]for the end. (I put those in(?:…)just to limit the scope of the|RE operator.)Demonstrating from an interactive session: