Ruby on rails 如何使用正则表达式从json中查找日期和时间
您好,我正在使用ruby-2.5.0和Rails 5进行RoR项目。我有一个json,json中有一个日期和时间。我必须使用正则表达式从json中找到日期和时间。我的json如下:-Ruby on rails 如何使用正则表达式从json中查找日期和时间,ruby-on-rails,json,regex,Ruby On Rails,Json,Regex,您好,我正在使用ruby-2.5.0和Rails 5进行RoR项目。我有一个json,json中有一个日期和时间。我必须使用正则表达式从json中找到日期和时间。我的json如下:- { "Lines": [{ "Words": [{ "WordText": "WELCOME", "Left": 655, "Top": 442, "Hei
{
"Lines": [{
"Words": [{
"WordText": "WELCOME",
"Left": 655,
"Top": 442,
"Height": 23,
"Width": 91
}],
"MaxHeight": 23,
"MinTop": 442
},
{
"Words": [{
"WordText": "0102770005",
"Left": 752,
"Top": 1621,
"Height": 23,
"Width": 131
}],
"MaxHeight": 23,
"MinTop": 1621
},
{
"Words": [
{
"WordText": "06/02/18",
"Left": 581,
"Top": 1826,
"Height": 19,
"Width": 78
},
{
"WordText": "16:25",
"Left": 671,
"Top": 1826,
"Height": 20,
"Width": 46
}
],
"MaxHeight": 20,
"MinTop": 1826
},
{
"Words": [{
"WordText": "FOR",
"Left": 620,
"Top": 1785,
"Height": 22,
"Width": 37
},
{
"WordText": "YOUR",
"Left": 672,
"Top": 1785,
"Height": 22,
"Width": 51
},
{
"WordText": "CUSTOM",
"Left": 738,
"Top": 1785,
"Height": 22,
"Width": 79
}
],
"MaxHeight": 22,
"MinTop": 1785
},
{
"Words": [{
"WordText": "rpos",
"Left": 477,
"Top": 1915,
"Height": 22,
"Width": 48
}],
"MaxHeight": 22,
"MinTop": 1915
},
{
"Words": [{
"WordText": "C",
"Left": 447,
"Top": 1948,
"Height": 22,
"Width": 22
},
{
"WordText": "ASH",
"Left": 477,
"Top": 1947,
"Height": 23,
"Width": 72
},
{
"WordText": "OUT",
"Left": 577,
"Top": 1947,
"Height": 22,
"Width": 79
}
],
"MaxHeight": 23,
"MinTop": 1947
},
{
"Words": [{
"WordText": "315.47",
"Left": 911,
"Top": 1915,
"Height": 22,
"Width": 79
}],
"MaxHeight": 22,
"MinTop": 1915
},
{
"Words": [{
"WordText": "GST:",
"Left": 724,
"Top": 1979,
"Height": 23,
"Width": 51
}],
"MaxHeight": 23,
"MinTop": 1979
},
{
"Words": [{
"WordText": "402!",
"Left": 479,
"Top": 2209,
"Height": 22,
"Width": 46
},
{
"WordText": "18",
"Left": 530,
"Top": 2208,
"Height": 23,
"Width": 23
},
{
"WordText": "II",
"Left": 570,
"Top": 2210,
"Height": 21,
"Width": 20
},
{
"WordText": ".28",
"Left": 597,
"Top": 2208,
"Height": 23,
"Width": 37
}
],
"MaxHeight": 23,
"MinTop": 2208
}
],
"HasOverlay": true,
"Message": "Total lines: 42"
}
"WordText": "08/02/18" and "WordText": "16:25"
json中有WordText,其中包含日期“08/02/18”和时间“16:25”,如下所示:-
{
"Lines": [{
"Words": [{
"WordText": "WELCOME",
"Left": 655,
"Top": 442,
"Height": 23,
"Width": 91
}],
"MaxHeight": 23,
"MinTop": 442
},
{
"Words": [{
"WordText": "0102770005",
"Left": 752,
"Top": 1621,
"Height": 23,
"Width": 131
}],
"MaxHeight": 23,
"MinTop": 1621
},
{
"Words": [
{
"WordText": "06/02/18",
"Left": 581,
"Top": 1826,
"Height": 19,
"Width": 78
},
{
"WordText": "16:25",
"Left": 671,
"Top": 1826,
"Height": 20,
"Width": 46
}
],
"MaxHeight": 20,
"MinTop": 1826
},
{
"Words": [{
"WordText": "FOR",
"Left": 620,
"Top": 1785,
"Height": 22,
"Width": 37
},
{
"WordText": "YOUR",
"Left": 672,
"Top": 1785,
"Height": 22,
"Width": 51
},
{
"WordText": "CUSTOM",
"Left": 738,
"Top": 1785,
"Height": 22,
"Width": 79
}
],
"MaxHeight": 22,
"MinTop": 1785
},
{
"Words": [{
"WordText": "rpos",
"Left": 477,
"Top": 1915,
"Height": 22,
"Width": 48
}],
"MaxHeight": 22,
"MinTop": 1915
},
{
"Words": [{
"WordText": "C",
"Left": 447,
"Top": 1948,
"Height": 22,
"Width": 22
},
{
"WordText": "ASH",
"Left": 477,
"Top": 1947,
"Height": 23,
"Width": 72
},
{
"WordText": "OUT",
"Left": 577,
"Top": 1947,
"Height": 22,
"Width": 79
}
],
"MaxHeight": 23,
"MinTop": 1947
},
{
"Words": [{
"WordText": "315.47",
"Left": 911,
"Top": 1915,
"Height": 22,
"Width": 79
}],
"MaxHeight": 22,
"MinTop": 1915
},
{
"Words": [{
"WordText": "GST:",
"Left": 724,
"Top": 1979,
"Height": 23,
"Width": 51
}],
"MaxHeight": 23,
"MinTop": 1979
},
{
"Words": [{
"WordText": "402!",
"Left": 479,
"Top": 2209,
"Height": 22,
"Width": 46
},
{
"WordText": "18",
"Left": 530,
"Top": 2208,
"Height": 23,
"Width": 23
},
{
"WordText": "II",
"Left": 570,
"Top": 2210,
"Height": 21,
"Width": 20
},
{
"WordText": ".28",
"Left": 597,
"Top": 2208,
"Height": 23,
"Width": 37
}
],
"MaxHeight": 23,
"MinTop": 2208
}
],
"HasOverlay": true,
"Message": "Total lines: 42"
}
"WordText": "08/02/18" and "WordText": "16:25"
我必须使用正则表达式查找日期“08/02/18”和时间“16:25”。JSON格式可以不同,但键和值将相同。请帮助我实现这一目标。提前感谢。您可以尝试使用正则表达式从JSON中查找日期和时间
var对象行={
“行”:[{
“文字”:[{
“WordText”:“欢迎”,
“左”:655,
“顶级”:442,
“高度”:23,
“宽度”:91
}],
“最大高度”:23,
“MinTop”:442
},
{
“文字”:[{
“WordText”:“0102770005”,
“左”:752,
“顶级”:1621,
“高度”:23,
“宽度”:131
}],
“最大高度”:23,
“明托普”:1621
},
{
“文字”:[
{
“WordText”:“06/02/18”,
“左”:581,
“顶级”:1826年,
“高度”:19,
“宽度”:78
},
{
“文字文字”:“16:25”,
“左”:671,
“顶级”:1826年,
“高度”:20,
“宽度”:46
}
],
“最大高度”:20,
“明托普”:1826
},
{
“文字”:[{
“WordText”:“FOR”,
“左”:620,
“顶级”:1785年,
“高度”:22,
“宽度”:37
},
{
“WordText”:“您的”,
“左”:672,
“顶级”:1785年,
“高度”:22,
“宽度”:51
},
{
“WordText”:“自定义”,
“左”:738,
“顶级”:1785年,
“高度”:22,
“宽度”:79
}
],
“最大高度”:22,
“明托普”:1785年
},
{
“文字”:[{
“WordText”:“RPO”,
“左”:477,
“顶级”:1915年,
“高度”:22,
“宽度”:48
}],
“最大高度”:22,
“明托普”:1915年
},
{
“文字”:[{
“WordText”:“C”,
“左”:447,
“顶级”:1948年,
“高度”:22,
“宽度”:22
},
{
“文字”:“灰烬”,
“左”:477,
“顶级”:1947年,
“高度”:23,
“宽度”:72
},
{
“WordText”:“OUT”,
“左”:577,
“顶级”:1947年,
“高度”:22,
“宽度”:79
}
],
“最大高度”:23,
“明托普”:1947年
},
{
“文字”:[{
“WordText”:“315.47”,
“左”:911,
“顶级”:1915年,
“高度”:22,
“宽度”:79
}],
“最大高度”:22,
“明托普”:1915年
},
{
“文字”:[{
“WordText”:“GST:”,
“左”:724,
“顶级”:1979年,
“高度”:23,
“宽度”:51
}],
“最大高度”:23,
“明托普”:1979年
},
{
“文字”:[{
“WordText”:“402!”,
“左”:479,
“顶级”:2209,
“高度”:22,
“宽度”:46
},
{
“WordText”:“18”,
“左”:530,
“顶级”:2208,
“高度”:23,
“宽度”:23
},
{
“WordText”:“II”,
“左”:570,
“顶级”:2210,
“高度”:21,
“宽度”:20
},
{
“文字”:“.28”,
“左”:597,
“顶级”:2208,
“高度”:23,
“宽度”:37
}
],
“最大高度”:23,
“MinTop”:2208
}
],
“HasOverlay”:没错,
“消息”:“总行数:42”
};
风险值日期;
var时间;
objectLines.Lines.forEach(函数(元素){
元素.Words.forEach(函数(el){
var dateRx=/^(\d{1,2})\/(\d{1,2})\/(\d{2})$/;
如果(el.WordText.match(dateRx)){
日期=el.WordText;
}
var timeRx=/^(\d{1,2}):(\d{1,2})$/;
if(el.WordText.match(timeRx)){
时间=el.WordText;
}
});
});
控制台日志(日期);
console.log(时间);
var reselt={“日期”:日期+''+时间};
控制台日志(重新发送)
在您的案例中,这里是查找日期
或时间
的正则表达式:
pattern = /\A(\d{2}:\d{2}|\d{2}\/\d{2}\/\d{2})\z/
WordText = '06/02/18'
WordText[pattern] # => '06/02/18'
WordText = '16:25'
WordText[pattern] # => '16:25'
WordText = 'WELCOME'
WordText[pattern] # => nil
我将添加一些空白以便更具可读性
pattern = /\A ( \d{2}:\d{2} | \d{2}\/\d{2}\/\d{2} ) \z/
\A
和\z
:字符串的开始和结束
\d
:是任意数字
\d{2}
:是否有任何两位数字Hi@awsm,您能给我看一下您预期的输出json吗?我必须在我的输出json中只返回日期/时间,如{“date”:“08/02/18 16:25”}如果有两行包含日期和时间怎么办?只需返回日期和时间的值,我将自己的日期和时间组合在一起。是的,只需告诉我答案即可