In the general case, you probably cannot. The easiest approach is to match everything and use backlinks to capture the part of interest:
Foo\s+(Bar)\s+Baz
This is not the same as containing no surrounding text in the match. It probably doesn't matter if all you want to do is extract the βBarβ, but it will matter if you match the same line multiple times and you need to continue from where you left off previous match.
Look-around will work in some cases. Tomalak's offer:
(?<=Foo\s)Bar(?=\sBaz)
only works with fixed width (at least in Perl). Starting in Perl 5.10, the \K statement can be used to efficiently represent variable widths:
Foo\s+\KBar(?=\s+Baz)
which should be able to do everything you requested in all cases, but will require that you implement this in Perl 5.10.
While it would be convenient, there is no \K equivalent to complete the agreed text, so you need to use the "look ahead" option.
source share