ruby – Regexp搜索一个非常大的文件
我需要使用正则表达式扫描300MB的文本文件.
>读取整个文件并将其转换为变量会占用超过700MB的RAM,然后因“无法分配内存”错误而失败. 是否有任何惰性方法使用正则表达式进行完整文件扫描而不将其读入单独的变量? UPD 完成.现在您可以使用此功能来读取块. def prepare_session_hash(fname,regex_string,start=0) @session_login_hash = {} File.open(fname,'rb') { |f| fsize = f.size bsize = fsize / 8 if start > 0 f.seek(start) end overlap = 200 while true if (f.tell() >= overlap) and (f.tell() < fsize) f.seek(f.tell() - overlap) end buffer = f.read(bsize) if buffer buffer.scan(s) { |match| @session_login_hash[match[0]] = match[1] } else return @session_login_hash end end } end 解决方法
>以块的形式遍历文件,而不是逐行遍历,其中块是由频繁出现的字符或模式(例如“X”)创建的.
>“X”是这样的,它在你的正则表达式中从不存在,即“X”是你的正则表达式永远不会与字符串匹配的地方. >在当前块中匹配正则表达式,提取匹配项并继续下一个块. 例: This is string with multline numbers -2000 2223434 34356666 444564646 . These numbers can occur at 34345 567567 places,and on 67 87878 pages . The problem is to find a good way to extract these more than 100 0 regexes without memory hogging. 在本文中,假设所需的模式是数字字符串,例如/ d / s匹配数字多行, 程序块#1: This is string with multline numbers -2000 2223434 34356666 444564646 . 程序块#2: These numbers can occur at 34345 567567 places,and on 67 87878 pages 等等. 编辑:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |