Have a look at this, it may help you Subroutine to Perform Search/Replace Using Variable Patterns
For more information, see the wiki Searching the Forum for Answers section.