免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: juzi1114
打印 上一主题 下一主题

如何提取介于某个区间的几行文字,区间的开始和结束可以用正则表达式描述 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2008-09-10 18:04 |只看该作者
刚把range  operator好好的看了一遍...

论坛徽章:
0
22 [报告]
发表于 2008-10-28 13:34 |只看该作者
原帖由 flw 于 2008-6-20 12:51 发表

向前断言,不匹配。




请教楼主,这边的?!是不是不匹配相应内容的意思???

还是有点理解不了,
(/$start/ .. /$end/) and !/$end/
从开始到结束,并且不包含结束,这本身不是矛盾吗?

我逻辑能力太差,能够请楼主简单举例说明!

呵呵!不好意思,弱弱的问题。

论坛徽章:
0
23 [报告]
发表于 2008-10-29 07:46 |只看该作者
再把这个顶出来

如果我想取出类似下面这样的怎么办
temp
testest
****SETTINGS****
test|/linux/bin
temp|/linux/etc
****SETTINGS****

testtestt
tmptmp
上下的标记都是一样的,所有range operator不行,有什么漂亮的招数么?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
24 [报告]
发表于 2008-10-29 10:04 |只看该作者
to 楼上的,
range opreator 有两个版本:两个点的和三个点的。你用三个点就可以了。

论坛徽章:
0
25 [报告]
发表于 2008-10-29 19:41 |只看该作者
Beautiful, many thinks to FLW.

论坛徽章:
0
26 [报告]
发表于 2009-08-17 14:19 |只看该作者

回复 #24 flw 的帖子

版主,能讲讲range opreator 吗?

论坛徽章:
0
27 [报告]
发表于 2011-01-03 14:41 |只看该作者
Perl Range Operator
in a list context, this operator is easy to use and understand:  .. return a list of values counting (by ones) from the left value to the right value. This is useful for wrting for (1 .. 10) loops and for doing slice operations on arrays:

    @foo = @foo[-5 .. -1]; #slice last 5 items.
#!/usr/bin/perl
foreach (1..100)
{
   print;
}

foreach (A..B)
{
  print;
}
It is much more confusing in a scalar context. Let's look at the simplest cases first and then move to the more difficult:
#!/usr/bin/perl
while(<>)
{
   print "$. $_" if 3 .. 10;
}
That prints lines 3 to 10 of any input of file given to it. if we change it to print "$. $_" if 3 ... 10; it works the same way. But we aren't limited to line numbers. Let's create a file "g":
# stuff
# stuff
# end on same line as start
START foo  END
#
# end by itself
END
# more stuff
# more stuff
# another start
START
# and more stuff
END
# trailing stuff     


and a perl script to read it:
#!/usr/bin/perl

while(<>)
{
   print if(/START/../END/);
}
Running that against our file produces:
START foo  END
START
# and more stuff
END

But with the three dot version:
#!/usr/bin/perl

while(<>)
{
  print if(/START/.../END/);
}
the results are different:
START foo  END
#
# end by itself
END
START
# and more stuff
END

The difference is how the operator treats its left side and right side when they appear on the same line or out of order.Here's what's going on: with the two dot ".." version, the operator returns false until the left hand side is true. It then switches to returnning true and keeps doing that until its right hand expression evaluates to true. But the operator $EMAINS true until the next evaluation even so, so it only switches back to false when the next line is read. with the three dot "..." version, the right hand expression will not evaluated while the left hand is false, and vice versa. Confusing? you bet .. be very careful when using these opeartors and test your logic against sample input very carefully.



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/emili/archive/2010/02/20/5312998.aspx

论坛徽章:
0
28 [报告]
发表于 2011-01-03 15:50 |只看该作者
我也来个,不要区间Start和end行的话
perl -ne '$in = 1 and next if /START/; $in = 0 and exit if /END/; print if $in'
要的话,稍微改下就是了。不过还是(/$start/ .. /$end/)简单些。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP