如何在 Perl 中获取连续的单词对
用这句话:
my $sent = "Mapping and quantifying mammalian transcriptomes RNA-Seq";
我们想要得到所有可能的连续的单词对。
my $var = ['Mapping and',
'and quantifying',
'quantifying mammalian',
'mammalian transcriptomes',
'transcriptomes RNA-Seq'];
有没有一种紧凑的方法来做到这一点?
With this sentence:
my $sent = "Mapping and quantifying mammalian transcriptomes RNA-Seq";
We want to get all possible consecutive pairs of words.
my $var = ['Mapping and',
'and quantifying',
'quantifying mammalian',
'mammalian transcriptomes',
'transcriptomes RNA-Seq'];
Is there a compact way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是可行的:
即只需将原始字符串拆分为单词数组,然后使用
map
迭代生成所需的对。This works:
i.e. just split the original string into an array of words, and then use
map
to iteratively produce the desired pairs.我没有将其作为一行,但以下代码应该为您提供开始的地方。基本上是通过
push
和带有/g
的正则表达式来完成的。感谢 @ysth 的语法,
我的正则表达式略有不同,因为如果您有奇数个单词,最后一个单词仍然会获得一个条目。
I don't have it as a single line, but the following code should give you somewhere to start. Basically does it with a
push
and a regext with/g
.One liner thanks to the syntax from @ysth
My regex is slightly different in that if you have an odd number of words, the last word still gets an entry.
是的。
Yes.
一种依赖于运算符评估顺序但不依赖于奇特的正则表达式或索引的变体(可能是不明智的):
A variation that (perhaps unwisely) relies on operator evaluation order but doesn't rely on fancy regexes or indices: