Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在perl中用扩展表达式替换多行不起作用_Regex_Perl - Fatal编程技术网

Regex 在perl中用扩展表达式替换多行不起作用

Regex 在perl中用扩展表达式替换多行不起作用,regex,perl,Regex,Perl,我试图解析以下多行字符串(以开头)并注释掉它 --> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread

我试图解析以下多行字符串(以开头)并注释掉它

    -->
<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
-->
因此,我尝试使用以下方法:

perl -i.bak -pe 'BEGIN{undef $/;}
        s/
            \s+ #space at beginning of line
            (<Connector\s+ #connector
             port=\"8080\"\s+  #port
             protocol=\"HTTP\/1\.1\" #protocol
             \n  #newline
            \s+connectionTimeout=\"20000\"\n # space, connection timeout, then newline
            \s+redirectPort=\"8443\" #redirect port
            \/> # end of connector entry in file
            ) # end capture for $1
        /
            <!--$1-->
        /msx
    ' server.xml

diff server.xml server.xml.bak
perl-i.bak-pe'BEGIN{unde$/;}
/
\行首的s+#空格
(#文件中连接器条目的结尾
)#以1美元结束捕获
/
/msx
'server.xml
diff server.xml server.xml.bak

但是diff输出没有显示任何内容。知道我在这里遗漏了什么吗?

我想我知道了

perl -i.bak -pe 'BEGIN{undef $/;}
        s/
            --> #preceding line ends a comment, with newline at end
            \s+ #space at beginning of line
            (<Connector\s+ #connector
             port=\"8080\"\s+  #port
             protocol=\"HTTP\/1\.1\" #protocol
            \s+connectionTimeout=\"20000\" # space, connection timeout, then newline
            \s+redirectPort=\"8443\" #redirect port
            \s+   #space
            \/> # end of connector entry in file
            ) # end capture for $1
        /
            -->\n<!-- $1 -->
        /msx
    ' server.xml

diff server.xml server.xml.bak
~
perl-i.bak-pe'BEGIN{unde$/;}
/
-->#前一行以注释结尾,末尾为换行符
\行首的s+#空格
(#文件中连接器条目的结尾
)#以1美元结束捕获
/
-->\n
/msx
'server.xml
diff server.xml server.xml.bak
~

不要使用BEGIN块。在文本文件中发出咕噜声的正常方法是使用
-0
开关。将输入记录分隔符设置为空字符的。如果文件中可能存在空值,请使用
-0777

如果您确切地知道搜索文本是什么,那么就不需要像您编写的那样复杂的内容。Perl已经涵盖了这个用例。
\Q\E
运算符自动引用任何可能出现问题的字符,但仍允许进行变量替换。
$foo='f.oo bar$';打印qr/\Q$foo\E/
(?^:f\.oo\bar\$)

$pattern=qr{\Q\E};
$text=~s/($pattern)/;
我知道你想把它作为一个命令行,所以它应该是这样的

perl -i.bak -lp0e '$pattern = qr{\Q<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />\E};
s/($pattern)/<!-- $1 -->/; ' FILE
perl-i.bak-lp0e'$pattern=qr{\Q\E};
s/($pattern)/;'文件
您输入的代码将只执行一次,因为输入只有“一行”

如果在空白量上有回旋余地,您可以对模式本身进行动态替换

$pattern=qq{\Q\E};
#将转义空间的运行转换为单个\s+
$pattern=~s/(?:\\\s)+/\s+/g;
$text=~s/($pattern)/;

HTH

使用会说XML的东西,而不是正则表达式。也不要使用
-0
。这并不是你想要的。使用
-0777
(这与
$/=undef;
相同)…我注意到您后来提到了如何正确操作,那么您为什么要费心提到错误的方法呢?Re“-0是99%规则之一。只需记住1%。”,什么?为什么我会想用错误的东西,因为它只失败1%的时间?那完全是胡说八道。为什么呢?为了节省3个字符?这怎么能保证所有的混乱呢?使用的东西,工作100%的时间!Re“该\Q\E运算符会自动引用任何可能出现问题的字符。”,不完全正确。它不会引用
$
@
\
的某些实例,也不会阻止它们的效果。您自己说过它不会随机工作。这正是错误的定义。TIMTOWTDI建议其他样式的公差。这不是说接受错误的解决方案,这是真的。就像你刚才说的
$
@
\Q..\E
中仍然受到特殊对待(至少在某些时候)。他们仍然执行与您最初所说相反的插值。