在perl中删除行
我是 Perl 新手,以前没有任何其他编程语言的编程经验。我正在尝试使用 do-while 循环删除/跳过几行。我正在尝试删除/跳过两个标签之间的数据:
和 。
代码:
if($work=/^<worker>/)
{
do
{
delete $work[$i];
++$i;
}
print $work,"\n";
}
I am new to perl and have no previous programming experience with any other programming language. I am trying to delete/skip few line using a do-while loop. I am trying to delete/skip data between two tags: <worker>
and </workers>
.
Code:
if($work=/^<worker>/)
{
do
{
delete $work[$i];
++$i;
}
print $work,"\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您想跳过标签
和之间的数据。你需要在正则表达式中使用 ..
这是一个单行代码可以做到这一点
如果你想在脚本中执行它,请在从文件中读取一行后添加这一行
你可以用这个非常好的 演示,作者:brian d foy
I am assuming you want to skip data between tags
<workers>
and</workers>
. You need to use .. in regular expressionThis is a one liner that does it
If you want to do it in the script, add this line after reading a line from the file
You can start perl with this very good presentation by brian d foy
在不同的标签上开始和结束似乎有点奇怪。他们应该都是“工人”还是“工人”?
Seems slightly strange to start and end on different tags. Should they both be 'worker' or 'workers'?
我假设您有如下数据:
如果您还想删除
标签:如果您需要保留标签:
但是,只有当您可以保证 < code>标签不嵌套。以下字符串将导致错误:
要真正好且安全地完成这项工作,您需要使用诸如
HTML::TreeBuilder
或XML::Twig
之类的解析模块I assume you have data like:
If you want to remove
<worker></worker>
tags as well:If you need to keep tags:
However, this will work only if you can guarantee
<worker>
tags are not nested. Following string will cause error:To do this work really good and safe you need to use parsing modules like
HTML::TreeBuilder
orXML::Twig