简体   繁体   English

使用sed替换

[英]Using sed to replace <? with <?php

I'm trying to programmatically replace <? 我正在尝试以编程方式替换<? with <?php in a bunch of file, but my sed regex isn't behaving like I expected. 在一堆文件中使用<?php ,但我的sed正则表达式并不像我预期的那样。 Can you tell me what's wrong with it? 你能告诉我它有什么问题吗?

I'm testing it on the command line here: 我在命令行测试它:

$ sed -e 's/<\?/<\?php/g'
<?
<?php?<?php
d
<?phpd<?php

我不认为你需要逃脱吗?:

sed -e 's/<?/<?php/g'

You don't need to escape the back reference in the replacement. 您无需在替换中转义后引用。

sed 's#<\?#<?php#'

In a pipe, to correct for doubling the php: 在管道中,纠正加倍的PHP:

sed 's#<\?#<?php#g' | sed 's#phpphp#php#g'

这也将跳过与<?=$variable匹配:

sed -e 's/<?\([^=p]\)/<?php\1/g' -e 's/<?$/<?php/g'

Do you really have to use sed, or can you use perl as well? 你真的必须使用sed,还是你也可以使用perl?

perl -pi.tmp -e 's,^<\?(?!php),<?php,' *.php *.inc
rm *.tmp

I am using a negative look-ahead to avoid generating <?phpphp in cases where the files already start with the correct characters. 我正在使用负面<?phpphp ,以避免在文件已经以正确的字符开头的情况下生成<?phpphp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM