in reply to backslash and word boundaries while matching

The problem is that if there is a space before "/usr/myname" in your string, there isn't a word boundary before the /, but if there is e.g. an "x" ("x/usr/myname"), there is.

So don't use \b, instead look for no word character before or after: /(?<!\w)$expectedoutput(?!\w)/.

Update: as Prior Nacre V suggests, you probably want to use \Q around $expectedoutput in case of special regex characters in your variable (e.g. "."), but that won't fix your \b problems.