字符串/日期的正则表达式

时间:2017-01-20 01:07:06

标签: sql-server regex powershell

我正在尝试找一个正则表达式来读取包含日期和时间的行,该行包含文本文件中的日期和时间以添加到如下所示的select语句中(取自here):

True

这是我尝试阅读的行的格式:

Thu 08/12/2016  0:15:00.15

1 个答案:

答案 0 :(得分:0)

您可以使用以下正则表达式:

([a-zA-Z]{3})\s+?(\d{2})\/(\d{2})\/(\d{4})\s+?(\d{1,2}):(\d{2}):(\d{2})\.(\d{2})

以下是JavaScript中的一个正在运行的示例:

var string = "There was something on Thu 08/12/2016  0:15:00.15. On Fri 09/12/2017 11:20:30.20 was something else.";
var regex = /([a-zA-Z]{3})\s+?(\d{2})\/(\d{2})\/(\d{4})\s+?(\d{1,2}):(\d{2}):(\d{2})\.(\d{2})/g;

var match = regex.exec(string);

while(match !== null) {
  console.log({
    match: match[0],
    weekDay: match[1],
    day: match[2],
    month: match[3],
    year: match[4],
    hour: match[5],
    minute: match[6],
    second: match[7],
    centiseconds: match[8]
  });
  
  match = regex.exec(string);
}