Redian新闻
>
Perl: conditionally match multiple lines
avatar
Perl: conditionally match multiple lines# Unix - 噫吁兮,危乎高哉
r*o
1
I only know how to read a file line by line
or read in the whole file as a string.
But how to read/match multiple lines conditionally?
e.g. a file contains certain lables:
xxx
yyy
Begin: zzz
aaa
bbb
Ene: ccc
ddd
I need the script to read/match multiple lines between the Begin/End
pair as a single string. But for other lines, just read
them one by one.
Is there any easy way to do this?
Thanks.
avatar
l*k
2
here is part of my code for this:
# it searches to the >$name, which is the alignment part for that hit
# it reads all info till the next > to $&
# $result get this info for further process
#
while($align=~ /^>$name.*\n(^(?!>).*\n)+/gm) {
$result = $&;
for your purpose, just change the second > into end, first > into begin, I bet
it should work.
and I am sure you are doing some sequence pars

【在 r*****o 的大作中提到】
: I only know how to read a file line by line
: or read in the whole file as a string.
: But how to read/match multiple lines conditionally?
: e.g. a file contains certain lables:
: xxx
: yyy
: Begin: zzz
: aaa
: bbb
: Ene: ccc

avatar
r*o
3
Sorry, I am a novice on Perl and don't quite understand your script.
I assume the $align is the big string for a whole file.
And I think it works to strip out multiple lines in a certain block,
but what about other lines? Can it read them out one by one?
(I think if only to match out certain blocks, isn't .. or ... operators
a better way?)
Another question is how to match nested blocks? e.g.
(block ...
(block ...
...
(block ...
))
)
Is there any way to match the whole outer bloc

【在 l*****k 的大作中提到】
: here is part of my code for this:
: # it searches to the >$name, which is the alignment part for that hit
: # it reads all info till the next > to $&
: # $result get this info for further process
: #
: while($align=~ /^>$name.*\n(^(?!>).*\n)+/gm) {
: $result = $&;
: for your purpose, just change the second > into end, first > into begin, I bet
: it should work.
: and I am sure you are doing some sequence pars

avatar
r*o
4
After lots of experiments and head-scratch,
I think I can now match the out-most block
(For clarity, I write it in multiple lines:
m{\(
.*?
\( [^\)]*
\)
}gx
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。