I have a string from file that looks like this:
我有一个文件中的字符串,如下所示:
"[[5473, 992, 'smth', Tue, 25 Mar 2014 05:08:12 UTC +00:00, Fri, 07 Mar 2014 22:55:42 UTC +00:00], [5473, 993, 'smth', Tue, 25 Mar 2014 14:38:05 UTC +00:00, Fri, 07 Mar 2014 22:57:33 UTC +00:00], [6084, 994, 'smth', Mon, 24 Mar 2014 23:37:37 UTC +00:00, Mon, 24 Mar 2014 23:37:37 UTC +00:00], [6084, 995, 'smth', nil, nil], [6084, 996, 'smth', nil, nil], [6084, 997, 'smth', nil, nil], [6084, 998, 'smth', nil, nil]]"
To correctly eval it as an array I need to wrap dates with quotes, I have created a regexp for cases when date is in the middle of an array:
要正确地将其作为数组进行评估,我需要用引号包装日期,我已经为日期位于数组中间的情况创建了一个正则表达式:
(\s)\w{3},\s\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\+00:00(,)
and when date ends with ]
symbol:
当日期以]符号结束时:
(\s)\w{3},\s\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\+00:00(])
now I want to replace \1
to " '"
and \2
to "',"
or "']"
correspondingly, with what ruby method I can accomplish my task?
现在我想将\ 1替换为“'”而将\ 2替换为“',”或“']”相应地,用什么ruby方法我可以完成我的任务?
2 个解决方案
#1
2
Your two regex has only one difference, the last character is ,
or ]
. So joined them in character class using [,\]]
. And use it in your ruby gsub()
like this way:
你的两个正则表达式只有一个区别,最后一个字符是,或者]。所以使用[,\]]在角色类中加入它们。并像这样在你的ruby gsub()中使用它:
input = input.gsub(/(\s)(\w{3},\s\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\+00:00)([,\]])/, " '\\2'\\3")
I have captured your date into group two \\2
and captured ,
or ]
in group three \\3
.
我已将您的日期捕获到第二组\\ 2并捕获,或者在第三组\\ 3中捕获。
Capturing the group 1 wasn't necessary, but I didn't remove it. If you remove the capturing, then the grouping will be shifted from \\2
to \\1
and \\3
to \\2
in above code.
没有必要捕获第1组,但我没有删除它。如果删除捕获,则分组将从上面的代码中的\\ 2转换为\\ 1和\\ 3转换为\\ 2。
#2
1
Why not convert the dates to Date
objects?
为什么不将日期转换为Date对象?
Code
码
require 'date'
regex = /\s(\w{3},\s\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\+00:00)/
s = str.gsub(regex, " Date.parse('\\1')")
and then
接着
arr = eval(s)
to create an array from the string s
. Notice that the regex ends with '00:00'
.
从字符串s创建一个数组。请注意,正则表达式以'00:00'结尾。
Demo
演示
str = "[[5473, 992, 'smth', Tue, 25 Mar 2014 05:08:12 UTC +00:00, Fri, 07 Mar 2014 22:55:42 UTC +00:00],
[5473, 993, 'smth', Tue, 25 Mar 2014 14:38:05 UTC +00:00, Fri, 07 Mar 2014 22:57:33 UTC +00:00],
[6084, 994, 'smth', Mon, 24 Mar 2014 23:37:37 UTC +00:00, Mon, 24 Mar 2014 23:37:37 UTC +00:00],
[6084, 995, 'smth', nil, nil],
[6084, 996, 'smth', nil, nil],
[6084, 997, 'smth', nil, nil],
[6084, 998, 'smth', nil, nil]]"
require 'awesome_print'
ap eval(str.gsub(regex, " Date.parse('\\1')"))
[
[0] [
[0] 5473,
[1] 992,
[2] "smth",
[3] #<Date: 2014-03-25 ((2456742j,0s,0n),+0s,2299161j)>,
[4] #<Date: 2014-03-07 ((2456724j,0s,0n),+0s,2299161j)>
],
[1] [
[0] 5473,
[1] 993,
[2] "smth",
[3] #<Date: 2014-03-25 ((2456742j,0s,0n),+0s,2299161j)>,
[4] #<Date: 2014-03-07 ((2456724j,0s,0n),+0s,2299161j)>
],
[2] [
[0] 6084,
[1] 994,
[2] "smth",
[3] #<Date: 2014-03-24 ((2456741j,0s,0n),+0s,2299161j)>,
[4] #<Date: 2014-03-24 ((2456741j,0s,0n),+0s,2299161j)>
],
[3] [
[0] 6084,
[1] 995,
[2] "smth",
[3] nil,
[4] nil
],
[4] [
[0] 6084,
[1] 996,
[2] "smth",
[3] nil,
[4] nil
],
[5] [
[0] 6084,
[1] 997,
[2] "smth",
[3] nil,
[4] nil
],
[6] [
[0] 6084,
[1] 998,
[2] "smth",
[3] nil,
[4] nil
]
]
#1
2
Your two regex has only one difference, the last character is ,
or ]
. So joined them in character class using [,\]]
. And use it in your ruby gsub()
like this way:
你的两个正则表达式只有一个区别,最后一个字符是,或者]。所以使用[,\]]在角色类中加入它们。并像这样在你的ruby gsub()中使用它:
input = input.gsub(/(\s)(\w{3},\s\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\+00:00)([,\]])/, " '\\2'\\3")
I have captured your date into group two \\2
and captured ,
or ]
in group three \\3
.
我已将您的日期捕获到第二组\\ 2并捕获,或者在第三组\\ 3中捕获。
Capturing the group 1 wasn't necessary, but I didn't remove it. If you remove the capturing, then the grouping will be shifted from \\2
to \\1
and \\3
to \\2
in above code.
没有必要捕获第1组,但我没有删除它。如果删除捕获,则分组将从上面的代码中的\\ 2转换为\\ 1和\\ 3转换为\\ 2。
#2
1
Why not convert the dates to Date
objects?
为什么不将日期转换为Date对象?
Code
码
require 'date'
regex = /\s(\w{3},\s\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\+00:00)/
s = str.gsub(regex, " Date.parse('\\1')")
and then
接着
arr = eval(s)
to create an array from the string s
. Notice that the regex ends with '00:00'
.
从字符串s创建一个数组。请注意,正则表达式以'00:00'结尾。
Demo
演示
str = "[[5473, 992, 'smth', Tue, 25 Mar 2014 05:08:12 UTC +00:00, Fri, 07 Mar 2014 22:55:42 UTC +00:00],
[5473, 993, 'smth', Tue, 25 Mar 2014 14:38:05 UTC +00:00, Fri, 07 Mar 2014 22:57:33 UTC +00:00],
[6084, 994, 'smth', Mon, 24 Mar 2014 23:37:37 UTC +00:00, Mon, 24 Mar 2014 23:37:37 UTC +00:00],
[6084, 995, 'smth', nil, nil],
[6084, 996, 'smth', nil, nil],
[6084, 997, 'smth', nil, nil],
[6084, 998, 'smth', nil, nil]]"
require 'awesome_print'
ap eval(str.gsub(regex, " Date.parse('\\1')"))
[
[0] [
[0] 5473,
[1] 992,
[2] "smth",
[3] #<Date: 2014-03-25 ((2456742j,0s,0n),+0s,2299161j)>,
[4] #<Date: 2014-03-07 ((2456724j,0s,0n),+0s,2299161j)>
],
[1] [
[0] 5473,
[1] 993,
[2] "smth",
[3] #<Date: 2014-03-25 ((2456742j,0s,0n),+0s,2299161j)>,
[4] #<Date: 2014-03-07 ((2456724j,0s,0n),+0s,2299161j)>
],
[2] [
[0] 6084,
[1] 994,
[2] "smth",
[3] #<Date: 2014-03-24 ((2456741j,0s,0n),+0s,2299161j)>,
[4] #<Date: 2014-03-24 ((2456741j,0s,0n),+0s,2299161j)>
],
[3] [
[0] 6084,
[1] 995,
[2] "smth",
[3] nil,
[4] nil
],
[4] [
[0] 6084,
[1] 996,
[2] "smth",
[3] nil,
[4] nil
],
[5] [
[0] 6084,
[1] 997,
[2] "smth",
[3] nil,
[4] nil
],
[6] [
[0] 6084,
[1] 998,
[2] "smth",
[3] nil,
[4] nil
]
]