Look ahead and Look behind in perlWith the look-ahead and look-behind constructs ,you can "roll your own" zero-width assertions to fit your needs. You can look forward or backward in the string being processed, and you can require that a pattern match succeed (positive assertion) or fail (negative assertion) there.
Every extended pattern is written as a parenthetical group with a question mark as the first character. The notation for the look-arounds is fairly mnemonic, but there are some other, experimental patterns that are similar, so it is important to get all the characters in the right order.
(?=pattern)is a positive look-ahead assertion
(?!pattern)is a negative look-ahead assertion
(?<=pattern)is a positive look-behind assertion
echo $mytmp2 uvw_abc uvw_def uvw_acb
echo $mytmp2 | perl -pe 's/uvw_(?=(abc|def))/xyz_/g' xyz_abc xyz_def uvw_acb
echo $mytmp2 | perl -pe 's/uvw_(?!(abc|def))/xyz_/g' uvw_abc uvw_def xyz_acb
echo $mytmp abc_uvw def_uvw acb_uvw
echo $mytmp | perl -pe 's/(?<=(abc|def))_uvw/_xyz/g' abc_xyz def_xyz acb_uvw
echo $mytmp | perl -pe 's/(?<!(abc|def))_uvw/_xyz/g' abc_uvw def_uvw acb_xyz