in reply to Re: Negating a leading whitespace
in thread Negating a leading whitespace

Your first example wouldn't match a line that began with XY, because it requires some character to precede it. Negative lookbehind or an alternation would fix:
/(?:^|\S)XY/ # or /(?<!\s)XY/
(I also prefer the builtin not-whitespace char, rather than negating the class).

Caution: Contents may have been coded under pressure.