[英]Using regex and sed command to substitute ' *' pointers in C++ code is not working
I'm a rookie programmer dealing with a C++ code that I should review and study. 我是一名新手程序员,负责处理我应该复习和研究的C ++代码。 One of the basic task I'm with is to change the pointers notification like this:
我要执行的基本任务之一是更改指针通知,如下所示:
char *name;
to this convention: 按照这个惯例:
char* name;
Since my OS is Ubuntu 13.10, I thought that regex could do the job. 由于我的操作系统是Ubuntu 13.10,因此我认为regex可以胜任。 I studied it a bit and tried different variations of a command like this:
我研究了一下,并尝试了类似以下命令的不同变体:
find ./ -type f -exec sed -i -e 's/(?<=\w)\s\*(?!\s)/\* /g' {} \;
That is intended to be read as: find in string an alphanumeric, then matches with whitespace followed by asterisk, when is followed by not-a-whitespace, and replace with asterisk and whitespace. 可以理解为:在字符串中找到一个字母数字,然后与空格匹配,然后是星号,当匹配时是非空格,然后是星号和空格。 I understand that for substitution \\s cannot be used, that look ahead/behind will not enter in the match, so I played a bit with command with little success.
我知道不能使用替代\\ s,所以向前/向后看不会进入比赛,所以我在命令上打了一点点,但收效甚微。 I tried it first with Eclipse IDE 'find' menu, and regular expression seems to be properly build since it's really finding what I expect.
我首先使用Eclipse IDE的“查找”菜单进行了尝试,正则表达式似乎可以正确构建,因为它确实可以找到我期望的东西。 But when I try the command, it's not changing the lines as expected, in fact they stand still the same, untouched.
但是,当我尝试执行该命令时,它并没有按预期方式更改行,实际上它们保持不变,没有被触碰。
I was also trying a narrower case of the above, ie to change casting to pointer like this: 我也在尝试上述情况的狭窄情况,即将强制类型更改为指针,如下所示:
(char *) another_name
into this 进入这个
(char*) another_name
using this command line: 使用以下命令行:
find ./ -type f -exec sed -i -e 's/(?<=\w)\s\*(?=\))/\*/g' {} \;
But seems that regex engine is not recognizing the escaped parenthesis, since it gives me "sed: -e expression #1, char 25: Unmatched ) or )". 但是正则表达式引擎似乎无法识别转义的括号,因为它给了我“ sed:-e表达式#1,char 25:Unmatched)或”)。
Now I'm banging my head to the wall trying to figure out which the correct way to handle this: why the substitution is not working as expected, and why the same command using isntead an escaped parenthesis inside the positive look behind is not recognized? 现在,我要敲打墙壁,试图找出解决该问题的正确方法:为什么替换未按预期工作,以及为什么在正向外观内使用istead和转义括号的同一命令无法识别?
Thanks in advance for any suggestion you can give. 预先感谢您的任何建议。
"a rookie programmer dealing with a C++ code" probably shouldn't be automating this. “处理C ++代码的新手程序员”可能不应该自动执行此操作。 Have you thought about the cases of
*
used for multiplication 您是否考虑过
*
用于乘法的情况
name * another_name
or dereference? 还是取消引用?
Type t = *it;
You seem to think C++ casts live inside round brackets 您似乎认为C ++强制转换放在圆括号内
(char*) name
where in fact they usualy appear inside angle ones 实际上,它们通常出现在角形内
static_cast<char*>(name)
etc .... 等等 ...
You might be better of doing this one by hand to better learn what you're dealing with. 您可能最好手动执行此操作,以更好地了解您正在处理的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.