免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 24039 | 回复: 27
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-20 12:13 |只看该作者 |倒序浏览
5可用积分
如这样的文件:
CC   -!- FUNCTION: Rapidly .
CC   -!- CATALYTIC ACTIVITY: Acetylcholine.
CC   -!- SUBUNIT: Homotetramer; composed .
CC       Interacts with PRIMA1.
CC       anchor it to the basal
CC       (By similarity).
CC   -!- SUBCELLULAR LOCATION: Cell junction, synapse. Secreted (By
CC       similarity). Cell membrane; Peripheral membrane protein (By
CC       similarity).
CC   -!- SUBCELLULAR LOCATION: Isoform 2: Cell membrane;
CC       anchor; Extracellular side (By similarity).
CC   -!- ALTERNATIVE PRODUCTS:
CC       Event=Alternative splicing; Named isoforms=2;
我要提取其中以SUBCELLULAR LOCATION开头的那一小段文件,如下:
SUBCELLULAR LOCATION: Cell junction, synapse. Secreted (By
  similarity). Cell membrane; Peripheral membrane protein (By
  similarity).
SUBCELLULAR LOCATION: Isoform 2: Cell membrane; Lipid-anchor, GPI-
   anchor; Extracellular side (By similarity).
我现在写的
f($line=~ /SUBCELLULAR LOCATION:/)
        {
                  print   $line,"\n";
        if ($array[$line_num]=~ /\-\!\-/)
        {next;}
                        }
        if( $array[$line_num] =~ /^\/\//)
   {
           $a++;
           last;

只有其中的第一句话出来 不知道怎么办才好啊 谢谢各位了!!

[ 本帖最后由 flw 于 2008-6-20 13:16 编辑 ]

最佳答案

查看完整内容

下面给出这一类问题的通用解决办法。这是面向行处理的一种轻量级解决方法。比那些对整个文件进行模式匹配的方法不知优雅了要多少倍。$start 表示开始标记的模式,$end 表示结束标记的模式,if ( (/$start/ .. /$end/) and !/$end/ ){表示需要开始和结束之间的,但不需要结束的那一行。运行结果:

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2008-06-20 12:13 |只看该作者
下面给出这一类问题的通用解决办法。

这是面向行处理的一种轻量级解决方法。
比那些对整个文件进行模式匹配的方法不知优雅了要多少倍。

$start 表示开始标记的模式,$end 表示结束标记的模式,
if ( (/$start/ .. /$end/) and !/$end/ ){
表示需要开始和结束之间的,但不需要结束的那一行。

  1. #! /usr/bin/env perl

  2. my $start = qr/^CC\s+-!- SUBCELLULAR LOCATION/;
  3. my $end = qr/^CC\s+-!- (?!SUBCELLULAR LOCATION)/;

  4. while(<DATA>){
  5.     if ( (/$start/ .. /$end/) and !/$end/ ){
  6.         print "*** $_";
  7.     }
  8.     else{
  9.         print "--- $_";
  10.     }
  11. }
  12. __END__
  13. CC   -!- FUNCTION: Rapidly .
  14. CC   -!- CATALYTIC ACTIVITY: Acetylcholine.
  15. CC   -!- SUBUNIT: Homotetramer; composed .
  16. CC       Interacts with PRIMA1.
  17. CC       anchor it to the basal
  18. CC       (By similarity).
  19. CC   -!- SUBCELLULAR LOCATION: Cell junction, synapse. Secreted (By
  20. CC       similarity). Cell membrane; Peripheral membrane protein (By
  21. CC       similarity).
  22. CC   -!- SUBCELLULAR LOCATION: Isoform 2: Cell membrane;
  23. CC       anchor; Extracellular side (By similarity).
  24. CC   -!- ALTERNATIVE PRODUCTS:
  25. CC       Event=Alternative splicing; Named isoforms=2;
复制代码

运行结果:
flw@debian:~$ ./ttt.pl
--- CC   -!- FUNCTION: Rapidly .
--- CC   -!- CATALYTIC ACTIVITY: Acetylcholine.
--- CC   -!- SUBUNIT: Homotetramer; composed .
--- CC       Interacts with PRIMA1.
--- CC       anchor it to the basal
--- CC       (By similarity).
*** CC   -!- SUBCELLULAR LOCATION: Cell junction, synapse. Secreted (By
*** CC       similarity). Cell membrane; Peripheral membrane protein (By
*** CC       similarity).
*** CC   -!- SUBCELLULAR LOCATION: Isoform 2: Cell membrane;
*** CC       anchor; Extracellular side (By similarity).

--- CC   -!- ALTERNATIVE PRODUCTS:
--- CC       Event=Alternative splicing; Named isoforms=2;
flw@debian:~$

论坛徽章:
0
3 [报告]
发表于 2008-06-20 12:29 |只看该作者
#!user/bin/perl

use strict;
use warnings;

my @data = <DATA>;
$_ = join '', @data;

my @t = /(SUBCELLULAR.*?)CC\s+-!-/msg;

print map {s/CC\s+//g; $_} @t;

__DATA__
CC   -!- FUNCTION: Rapidly .
CC   -!- CATALYTIC ACTIVITY: Acetylcholine.
CC   -!- SUBUNIT: Homotetramer; composed .
CC       Interacts with PRIMA1.
CC       anchor it to the basal
CC       (By similarity).
CC   -!- SUBCELLULAR LOCATION: Cell junction, synapse. Secreted (By
CC       similarity). Cell membrane; Peripheral membrane protein (By
CC       similarity).
CC   -!- SUBCELLULAR LOCATION: Isoform 2: Cell membrane;
CC       anchor; Extracellular side (By similarity).
CC   -!- ALTERNATIVE PRODUCTS:
CC       Event=Alternative splicing; Named isoforms=2;

[ 本帖最后由 cobrawgl 于 2008-6-20 12:36 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-06-20 12:40 |只看该作者

回复 #2 cobrawgl 的帖子

谢谢啦 呵呵.... 看来我的正则表达式还要多学习了 呵呵...

论坛徽章:
0
5 [报告]
发表于 2008-06-20 12:46 |只看该作者
版主有时间的话,给大家解说一下吧

论坛徽章:
0
6 [报告]
发表于 2008-06-20 12:49 |只看该作者
那个 ?! 是什么意思啊,版主

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
7 [报告]
发表于 2008-06-20 12:51 |只看该作者
原帖由 cobrawgl 于 2008-6-20 12:49 发表
那个 ?! 是什么意思啊,版主

向前断言,不匹配。

论坛徽章:
0
8 [报告]
发表于 2008-06-20 12:54 |只看该作者
谢谢版主指点,赶紧看书去了

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
9 [报告]
发表于 2008-06-20 13:08 |只看该作者
苯方法:

  1. #! /bin/perl

  2. use warnings;
  3. use strict;

  4. my $key;

  5. while(<DATA>){
  6.     if (/-!-/) {
  7.         $key = 0;
  8.     }
  9.     if (/SUBCELLULAR LOCATION/) {
  10.         print;
  11.         $key = 1;
  12.         next;
  13.     }
  14.     if ($key) {
  15.         print;
  16.     }   
  17. }

  18. __END__
  19. CC   -!- FUNCTION: Rapidly .
  20. CC   -!- CATALYTIC ACTIVITY: Acetylcholine.
  21. CC   -!- SUBUNIT: Homotetramer; composed .
  22. CC       Interacts with PRIMA1.
  23. CC       anchor it to the basal
  24. CC       (By similarity).
  25. CC   -!- SUBCELLULAR LOCATION: Cell junction, synapse. Secreted (By
  26. CC       similarity). Cell membrane; Peripheral membrane protein (By
  27. CC       similarity).
  28. CC   -!- SUBCELLULAR LOCATION: Isoform 2: Cell membrane;
  29. CC       anchor; Extracellular side (By similarity).
  30. CC   -!- ALTERNATIVE PRODUCTS:
  31. CC       Event=Alternative splicing; Named isoforms=2;
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2008-06-20 13:13 |只看该作者
原帖由 ly5066113 于 2008-6-20 13:08 发表
苯方法:

range operator 有一个内置的 state,相当于你的 $key,不过由它自己来维护,因此可读性更好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP