简体   繁体   English

在Perl oneliner中成功匹配的URL编码捕获值

[英]URL encode value of capture from successful match in Perl oneliner

I am using the following Perl oneliner in a bash script to capture multiline text between pre tags and add a second iteration of the tag contents wrapped in a different starting and ending string: 我在bash脚本中使用以下Perl oneliner,以捕获预标签之间的多行文本,并添加包装在不同起始和结束字符串中的标签内容的第二次迭代:

new_start_string="NEWSTART"
new_end_string="NEWEND"

perl -i -pe  "BEGIN{undef $/;} s/<pre>(.*?)<\/pre>/<pre>\$1<\/pre>${new_start_string}\$1${new_end_string}/smg" /path/to/file

It works perfectly for the task as specified. 它非常适合指定的任务。 I'm aware I'm parsing HTML with regex and that's not ideal, however the formatting of this text file is a known factor, very simple, under local control and done by local specification so it is a rare case in which parsing would be overkill and I've chosen not to do this via parsing. 我知道我正在用regex解析HTML,但这并不理想,但是,此文本文件的格式是一个已知因素,非常简单,在本地控制下并由本地规范完成,因此在极少数情况下会进行解析过度杀伤力,我选择不通过解析来执行此操作。

Using the same oneliner, how can I URL encode the second iteration of the capture value $1 variable? 使用相同的oneliner,如何对捕获值$ 1变量的第二次迭代进行URL编码? If it's impossible, is there another approach to get this result that is also relatively simple and readable? 如果不可能,是否还有另一种方法可以相对简单且可读地获得此结果?

For URI escaping you can use URI::Encode . 对于URI转义,您可以使用URI :: Encode To use a function inside the substitution, see the /e option. 要在替换内使用函数,请参见/e选项。

因为我更喜欢在Perl内核中使用模块,所以我最终使用了将e修饰符与CGI :: Util结合使用的oneliner:

perl -MCGI -i -pe  "BEGIN{undef $/;} s/<pre>(.*?)<\/pre>/'<pre>' . \$1 . '<\/pre>' . '$new_start_string' . CGI::escape(\$1) . '$new_end_string'/esmg" /my/path

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

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