Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
With lookbehind and lookahead you can match text when it is preceded
or followed by certain text. This is limited to text only: you can say,
“Find an f followed by a closing parenthesis” (using the expression
f(?=\))), but you can’t create an
expression that says, “Find an italic f followed by a roman closing
parenthesis.” In other words, any formatting that you set in Find Format
applies to the whole GREP.
However, with some GREP trickery it is possible to match text depending on the presence of formatting. It is easy to convert formatting codes to text tags (HTML-like tags, for instance). With such text tags you can then use lookaround to find roman text only when it is preceded or followed by a tag that represents italic text (or any other formatting, but we’ll use italic here for illustration). When you’re done, you convert from tags back to formatted text.