相当于 Ruby 中 Python 的 findall() 方法? [英] Equivalent to Python’s findall() method in Ruby?

查看:46
本文介绍了相当于 Ruby 中 Python 的 findall() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从列表中的模糊列表中提取所有 MP3 标题.

I need to extract all MP3 titles from a fuzzy list in a list.

使用 Python 这对我来说很好:

With Python this works for me fine:

import re
for i in re.compile('mmc.+?mp3').findall(open("tracklist.txt").read()): print i

我怎样才能在 Ruby 中做到这一点?

How can I do that in Ruby?

推荐答案

f = File.new("tracklist.txt", "r")
s = f.read
s.scan(/mmc.+?mp3/) do |track|
  puts track
end

这段代码的作用是打开文件进行读取并将内容作为字符串读入变量s.然后对字符串扫描正则表达式 /mmc.+?mp3/ (String#scan 收集所有匹配的数组),并打印它找到的每一个.

What this code does is open the file for reading and reads the contents as a string into variable s. Then the string is scanned for the regular expression /mmc.+?mp3/ (String#scan collects an array of all matches), and prints each one it finds.

这篇关于相当于 Ruby 中 Python 的 findall() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆