<?php echo $form->create(); ?>
<?php echo $form->hidden('id'); ?>
<?php echo $form->input('name')); ?>
<?php echo $form->submit('Save'); ?>
<?php echo $form->end(); ?>
I want to replace "; ?" with " ?".
我想替换“;?”用“?”。
I used the vi command ":%s/; \?/ \?/g" to do that. I got the following output
我使用vi命令“:%s /; \?/ \?/ g”来做到这一点。我得到了以下输出
<?php echo $form->create() ??>
<?php echo $form->hidden('id') ??>
<?php echo $form->input('name')) ??>
<?php echo $form->submit('Save') ??>
<?php echo $form->end() ??>
Actually, I need the following output.
实际上,我需要以下输出。
<?php echo $form->create() ?>
<?php echo $form->hidden('id') ?>
<?php echo $form->input('name')) ?>
<?php echo $form->submit('Save') ?>
<?php echo $form->end() ?>
Can you give the explanation for this strange behavior?
你能解释一下这种奇怪的行为吗?
2 个解决方案
#1
2
The \?
is the vi-regular-expression for the normal ?
in other program's regular expressions.
\?是正常的vi-regular-expression?在其他程序的正则表达式中。
You can look at :h regex
to verify this (or more precisely :h E61
).
您可以查看:h正则表达式来验证这一点(或更确切地说:h E61)。
So when using ; \?
you match ;
(no space) and ;<space>
(one space). From these matches the greediest one (see E61 - as many as possible) will be replaced with a ?
thus resulting in two question-marks (one new one and the old one) when there is a space present.
所以在使用时; \?你匹配; (没有空格)和;
The correct expression would be: s/; ?/ ?/g
正确的表达式是:s /; ?/ ?/G
EDIT: Fixed explanation to be more precise.
编辑:修正解释更精确。
#2
1
:%s/; \?/ \?/g
:%S /; \?/ \?/G
; \?
match ;<space>
zero or one time, and it replaces them with ?
.
So you get the original ?
with another ?
.
; \?匹配;
#1
2
The \?
is the vi-regular-expression for the normal ?
in other program's regular expressions.
\?是正常的vi-regular-expression?在其他程序的正则表达式中。
You can look at :h regex
to verify this (or more precisely :h E61
).
您可以查看:h正则表达式来验证这一点(或更确切地说:h E61)。
So when using ; \?
you match ;
(no space) and ;<space>
(one space). From these matches the greediest one (see E61 - as many as possible) will be replaced with a ?
thus resulting in two question-marks (one new one and the old one) when there is a space present.
所以在使用时; \?你匹配; (没有空格)和;
The correct expression would be: s/; ?/ ?/g
正确的表达式是:s /; ?/ ?/G
EDIT: Fixed explanation to be more precise.
编辑:修正解释更精确。
#2
1
:%s/; \?/ \?/g
:%S /; \?/ \?/G
; \?
match ;<space>
zero or one time, and it replaces them with ?
.
So you get the original ?
with another ?
.
; \?匹配;