In the general case, you probably can't. The simplest approach is to match everything and use backreferences to capture the portion of interest:
Foo\s+(Bar)\s+Baz
This isn't the same as not including the surrounding text in the match though. That probably doesn't matter if all you want to do is extract "Bar" but would matter if you're matching against the same string multiple times and need to continue from where the previous match left off.
Look-around will work in some cases. Tomalak's suggestion:
(?<=Foo\s)Bar(?=\sBaz)
only works for fixed width look-behind (at least in Perl). As of Perl 5.10, the \K
assertion can be used to effectively provide variable width look-behind:
Foo\s+\KBar(?=\s+Baz)
which should be capable of doing what you asked for in all cases, but would require that you're implementing this in Perl 5.10.
While it would be convenient, there's no equivalent of \K
for ending the matched text, so you have to use a look-ahead.