vi替换为两个问号而不是一个问号

时间:2022-02-08 22:24:18
<?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.

所以在使用时; \?你匹配; (没有空格)和; (一个空格)。从这些比赛中,最贪婪的(见E61 - 尽可能多)将被替换为?因此,当存在空间时,产生两个问号(一个新问号和一个旧问号)。

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.

所以在使用时; \?你匹配; (没有空格)和; (一个空格)。从这些比赛中,最贪婪的(见E61 - 尽可能多)将被替换为?因此,当存在空间时,产生两个问号(一个新问号和一个旧问号)。

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 ?.

; \?匹配; 零或一次,并用?替换它们。所以你得到原件?和另外一个 ?。