I have the following sample text and I want to replace ‘[core].’ with something else but I only want to replace it when it is not between text markers ‘ (SQL):
PRINT ‘The result of [core].[dbo].[FunctionX]’ + [core].[dbo].[FunctionX] + ‘.’
EXECUTE [core].[dbo].[FunctionX]
The Result shoud be:
PRINT ‘The result of [core].[dbo].[FunctionX]’ + [extended].[dbo].[FunctionX] + ‘.’
EXECUTE [extended].[dbo].[FunctionX]
I hope someone can understand this. Can this be solved by a regular expression?
With RegLove
Kevin
Not in a single step, and not in an ordinary text editor. If your SQL is syntactically valid, you can do something like this:
First, you remove every string from the SQL and replace with placeholders. Then you do your replace of
[core]with something else. Then you restore the text in the placeholders from step one:'(?:''|[^'])+'with'n', wherenis an index number (the number of the match). Store the matches in an array with the same number asn. This will remove all SQL strings from the input and exchange them for harmless replacements without invalidating the SQL itself.[core]. No regex required, normal search-and-replace is enough here.'1'with the first array item,'2'with the second, up ton. Now you have restored the original strings.The regex, explained:
JavaScript this would look something like this: