英文:
Dynamic sed to replace part of string
问题
以下是您要翻译的部分:
Well I have a project that was made upgrade from php 7.4 to 8.1
so many functions in someway doesn't work as before because in php 8.1 doesn't accept null anymore.
So I want to replace all functions with a script so I could automation it.
Example:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key); somethingAfter
testeBefore explode(somthing, $key); somethingAfter
line not affected after
Output:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? '''); somethingAfter
testeBefore explode(something, $key ?? '''); somethingAfter
line not affected after
So a solution could be add '?? ''' before close the parentesis of the function
here a snippet of what I tried to do: https://sed.js.org/?snippet=xqo9gd
I don't know if this is possible with sed.
If someone could help. I would appreciate :)
**EDIT:**
if snippet isn't available I will let here my try and output
I tried:
's/^(.??).explode(.)./explode(self::RULE_KEY_SEPARATOR, $key ?? ''''''');/g'
Output:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''');
explode(self::RULE_KEY_SEPARATOR, $key ?? ''');
line not affected after
As expected since is not dynamically...
英文:
Well I have a project that was made upgrade from php 7.4 to 8.1
so many functions in someway doesn't work as before because in php 8.1 doesn't accept null anymore.
So I want to replace all functions with a script so I could automation it.
Example:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key); somethingAfter
testeBefore explode(somthing, $key); somethingAfter
line not affected after
Output:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
testeBefore explode(something, $key ?? ''); somethingAfter
line not affected after
So a solution could be add ?? ''
before close the parentesis of the function
here a snippet of what I tried to do: https://sed.js.org/?snippet=xqo9gd
I don't know if this is possible with sed.
If someone could help. I would appreciate
EDIT:
if snippet isn't available I will let here my try and output
I tried:
's/^\(.*\?\?\).*explode\(.*\).*/explode(self::RULE_KEY_SEPARATOR, $key ?? '\'''\''\);/g'
Output:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? '');
explode(self::RULE_KEY_SEPARATOR, $key ?? '');
line not affected after
As expected since is not dynamically...
答案1
得分: 1
以下是翻译好的部分:
- 你创建了一个捕获组,但没有使用它,因此失去了所有前导空白。
- 在
explode
之后,你又创建了另一个捕获组,而不是字面匹配括号。 - 在第二个捕获组之后,你使用了贪婪正则表达式来捕获并丢弃行的其余部分。
- 你是在硬编码替换值,而不是使用已经存在的值。
你可以尝试使用这个sed
命令:
$ sed "s/explode([^)]*/& ?? ''/" input_file
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
testeBefore explode(somthing, $key ?? ''); somethingAfter
line not affected after
英文:
Your current command has a few issues;
- You create a capture group but do not utilize it therefore losing all leading whitespace
- After
explode
, you are creating another capture group rather than matching the parenthesis literally - After the second capture group, you use a greedy regex to capture and discard the remainder of the line.
- You are hardcoding the replace value rather than using the values already present
You can try this sed
$ sed "s/explode([^)]*/& ?? ''/" input_file
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
testeBefore explode(somthing, $key ?? ''); somethingAfter
line not affected after
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论