有\G吗?我只记得有一个/g是用于全局替换的,等等我查一下阿……wuchuanren 写了:突然想起来,perl里面有个叫\G(具体需要查书……)的标签,是从上次匹配结束开始匹配。
用这个就可以了
[问题]一个关于perl 的正则表达式的问题
- banban
- 帖子: 3340
- 注册时间: 2008-03-23 17:01
-
- 帖子: 99
- 注册时间: 2008-01-31 16:55
原来楼主还在关心,那我贴一段代码吧 :
呵呵,只是有个小地方不知道是否符合楼主的要求
代码: 全选
#!/usr/bin/perl -w
$data="atggcagttggtacctaagcattdggtacccgtta";
while($data =~ /\G.*?(.{4}ggtacc)/g){
print $1,"\n";
}
~
-
- 帖子: 99
- 注册时间: 2008-01-31 16:55
像这样做有个特点:
比如这么一串碱基
前后有两个ggtacc,后面一个ggtacc结尾的长度为10的串的前半部分含有上一个ggtacc的一部分内容,这段代码就不能完美解决
所以具体采用什么方法需要看楼主的需求:em01
(最近复习回一点perl啦~~)
比如这么一串碱基
代码: 全选
atggcagttggtaccttdggtacccgtta
所以具体采用什么方法需要看楼主的需求:em01
(最近复习回一点perl啦~~)

- banban
- 帖子: 3340
- 注册时间: 2008-03-23 17:01
-
- 帖子: 99
- 注册时间: 2008-01-31 16:55
-
- 帖子: 60
- 注册时间: 2006-05-01 12:29
- 来自: zju
有兴趣的话可以看mastering regular expressionbanban 写了:哦,这样的运行结果也是正确的。多谢你提供的另一种方法。
可是关于这个 \G ,我还是不知道它是如何用的,课本上没有讲到,也google 不到。不过,还是多谢了,呵呵。
里面很详细的
\G was first introduced by Perl to be useful when doing iterative matching with /g (☞51), and ostensibly matches the location where the previous match left off. On the first iteration, \G matches only at the beginning of the string, just like \A.
If a match is not successful, the location at which \G matches is reset back to the beginning of the string. Thus, when a regex is applied repeatedly, as with Perl's s/⋯/⋯/g or other language's "match all" function, the failure that causes the "match all" to fail also resets the location for \G for the next time a match of some sort is applied.
Type of match Where match starts pos upon success pos upon failure
m/⋯/ start of string (pos ignored) reset to undef reset to undef
m/⋯/g starts at target's pos set to end of match reset to undef
m/⋯/gc starts at target's pos set to end of match left unchanged
- banban
- 帖子: 3340
- 注册时间: 2008-03-23 17:01
多谢了哦……又有新的收获了,呵呵heejun 写了:有兴趣的话可以看mastering regular expressionbanban 写了:哦,这样的运行结果也是正确的。多谢你提供的另一种方法。
可是关于这个 \G ,我还是不知道它是如何用的,课本上没有讲到,也google 不到。不过,还是多谢了,呵呵。
里面很详细的\G was first introduced by Perl to be useful when doing iterative matching with /g (☞51), and ostensibly matches the location where the previous match left off. On the first iteration, \G matches only at the beginning of the string, just like \A.
If a match is not successful, the location at which \G matches is reset back to the beginning of the string. Thus, when a regex is applied repeatedly, as with Perl's s/⋯/⋯/g or other language's "match all" function, the failure that causes the "match all" to fail also resets the location for \G for the next time a match of some sort is applied.Type of match Where match starts pos upon success pos upon failure
m/⋯/ start of string (pos ignored) reset to undef reset to undef
m/⋯/g starts at target's pos set to end of match reset to undef
m/⋯/gc starts at target's pos set to end of match left unchanged