<?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
<?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?
The
\?is the vi-regular-expression for the normal?in other program’s regular expressions.You can look at
:h regexto verify this (or more precisely: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/; ?/ ?/gEDIT: Fixed explanation to be more precise.