0

I'm updating old code from a project and it has thousands of old php mysql_real_escape_string() calls.

The problem is I can't do a search replace because I need to keep the parameter in the () and then remove the closing ) from the call. Is this possible to do in an editor like sublime or vscode?

user2570937
  • 802
  • 3
  • 17
  • 34

2 Answers2

1

For your specific problem (I'm using Sublime Text 3)

Sample string:

mysql_real_escape_string(parameter) 

Find pattern (note we need to escape the parenthesis of the function):

mysql_real_escape_string\((.*?)\)

Replace:

new_fun(\1)

Which will result in:

new_fun(parameter) 

We match the old function, and capture the parameter in group 1. Then replace with the new function, and put the parameter (captured in group 1) inside using \1

Live example: https://regex101.com/r/OqOGGX/1

Mako212
  • 6,787
  • 1
  • 18
  • 37
0

You can use capturing groups in Sublime Text 3 search and replace. Thus, you can capture the parameter and use the back-reference to refer to it in replaced text.

Capturing groups in regexp use the special parenthesis characters to "capture" matches and make them available in "back-references" $1, $2 etc., which are like variables containing the matched group.

  1. Say we have a string:

SOMETHING_UNWANTEDMY_FIRST_CAPTURED_WORDSOMETHING_UNWANTEDMY_SECOND_CAPTURED_WORDSOMETHING_UNWANTED

  1. Find with regex:

SOMETHING_UNWANTED(MY_FIRST_CAPTURED_WORD)SOMETHING_UNWANTED(MY_SECOND_CAPTURED_WORD)SOMETHING_UNWANTED

  1. Replace using capture groups:

$1$2

  1. Result:

MY_FIRST_CAPTURED_WORDMY_SECOND_CAPTURED_WORD

miir
  • 1,814
  • 1
  • 17
  • 25