这是 Ruby 程序,我必须在其中使用正则表达式从文件中的数据中提取特定字段。 文件中的数据格式如下:
11 月 13 日 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [信息]:qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d 已被注销
我需要提取以下值 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"]
关于ruby - 从 Ruby 中的数据中提取特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34624713/