这是 Ruby程序,我必须使用文件中的数据使用正则表达式提取特定字段. 文件中的数据采用以下格式: 11月13日01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 0000 [info]:qas-296d1fa95f
文件中的数据采用以下格式:
11月13日01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 0000 [info]:qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d已取消注册adap_tdagt
我需要提取以下值
1)2015-11-13 01:46:57 0000
2)QAS-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d
我已经编写了代码,但它无法正常工作.有人可以帮我解决这个问题.
class Task5 def initialize # @f=File.open('C:/Users/aroraku/Desktop,boc-adap_td-agent.log-2.log',r) @count=0 end def check_line(line) if(line=~/deregistered adap_tdagt$/) line=~ (/.*(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} +\d{4})/) puts "#{$1}" end end def file_read open("boc-adap_td-agent.log-2.log") { |f| while line=f.gets do check_line(line) end } # return @count end end
str = "Nov 13 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [info]: qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d has been deregistered adap_tdagt"
由于您的代码问题已经确定,我想建议另一种方法从每行提取所需的信息:
r = / (?: # begin a non-capture group \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\+\d{4} # match date string ) # end non-capture group | # or (?: # begin a non-capture group (?<=\[info\]:\s) # match "[info:] " in a positive lookbehind \S+ # match >= 1 characters other than whitespace ) # end non-capture group /x # extended/free-spacing regex definition mode str.scan(r) #=> ["2015-11-13 01:46:57 +0000", "qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d"]