代码之家  ›  专栏  ›  技术社区  ›  simnom

PHP Regular Expression to extract timestamp and comment

  •  3
  • simnom  · 技术社区  · 15 年前

    我有许多从旧的Access数据库导出的文本字段,它们被移植到新的MySQL结构中。有各种格式的字段输入:

    2010年6月10日09:10:40工作尚未开始

    我想使用这个字符串,并使用某种正则表达式来提取日期/时间信息,然后提取注释。

    是否有简单的正则表达式语法来匹配此信息?

    谢谢

    9 回复  |  直到 14 年前
        1
  •  3
  •   jfoucher    15 年前

    我想我要试试这个

    preg_match('|^([0-9]{2})/([0-9]{2})/([0-9]{4})\s([0-9]{2}):([0-9]{2}):([0-9]{2})\s(.*)$|',$str,$matches);
    list($str,$d,$m,$y,$h,$m,$s,$comment)=$matches;
    

    然后,您就可以使用所需的值以任何格式重建时间。

        2
  •  7
  •   Sjoerd    15 年前

    您可以使用这个来代替regex:

    $parts = explode(" ", $string, 3);
    
        3
  •  3
  •   Konrad Rudolph    15 年前

    如我所见,您可以使用现有的空格作为分隔符,生成以下表达式:

    /([^ ]+) ([^ ]+) (.+)/
    

    也就是说:三个由空格分隔的组,其中前两个组不包含任何空格(但第三个5月)。

        4
  •  2
  •   Ain Tohvri    15 年前

    在这种情况下,Regex是昂贵的 . 如果这是始终保证存在的格式,则可以将其拆分为2个空格,并使用前2个切片,如下所示:

    $str = "10/06/2010 09:10:40 Work not yet started";
    $slices = explode(" ", $str, 3);
    $timestamp = strtotime($slices[0] . $slices[1]);
    echo "String is $str\n";
    echo "Timestamp is $timestamp\n";
    echo "Timestamp to date is " . strftime("%d.%m.%Y %T", $timestamp) . "\n";
    
        5
  •  1
  •   Austin Hyde    15 年前

    如果你的日期/时间是以类型存储的 datetime ,然后您可以使用

    preg_match("/^([0-9\\/]{10} [0-9:]{8}) (.*)$/",$str,$matches);
    $datetime = $matches[1];
    $description = $matches[2];
    

    如果您单独存储日期/时间,则可以使用

    preg_match("/^([0-9\\/]{10}) ([0-9:]{8}) (.*)$/",$str,$matches);
    $date = $matches[1];
    $time = $matches[2];
    $description = $matches[3];
    

    当然,正则表达式的另一种选择是分解字符串:

    list($date,$time,$description) = explode(' ',$str,3);
    

    另外一种选择,假设日期和时间总是相同的长度:

    $date = substr($str,0,10);
    $time = substr($str,11,19);
    $description = substr($str,20);
    
        6
  •  0
  •   Palantir    15 年前
    if(preg_match('([0-9/]+ [0-9:]+)', $myString, $regs)) {
      $myTime = strtotime($regs[1]);
    }
    
        7
  •  0
  •   cmendoza    15 年前

    如果只想将其提取为2个字符串,可以使用:

    ([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})\s(.*)
    
        8
  •  0
  •   Koala Yeung    15 年前

    您可以使用以下代码提取信息:

    // sample string you provided
    $string = "10/06/2010 09:10:40 Work not yet started";
    
    // regular expression to use
    $regex = "/^(\d+)\/(\d+)\/(\d+) (\d+)\:(\d+)\:(\d+) (.+?)$/";
    

    现在,您想要的所有字段都在数组$matches中。 要将信息提取到数组$matches中,可以使用preg_match()。

    // method 1: just extract
    preg_match($regex, $string, $matches);
    
    // method 2: to check if the string matches the format you provided first
    //           then do something with the extracted text
    if (preg_match($regex, $string, $matches) > 0) {
       // do something
    }
    

    要进一步使用您所掌握的信息:

    // to get a Unix timestamp out of the matches
    // you may use mktime()
    
    // method 1: supposed your date format above is dd/mm/yyyy
    $timestamp = mktime($matches[4], $matches[5], $matches[6], 
      $matches[2], $matches[1], $matches[3]);
    
    // method 2: or if your date format above is mm/dd/yyyy
    $timestamp = mktime($matches[4], $matches[5], $matches[6], 
      $matches[1], $matches[2], $matches[3]);
    

    然后,您可能希望查看时间是否正确解析:

    print date('r', $timestamp)
    

    最后,得到这样的评论:

    $comment = $matches[7];
    

    注意时区问题。如果您在生成这些数据的同一台服务器上分析这些数据,您很可能会没事的。您可能需要从上面的时间戳中添加/减去时间。

        9
  •  0
  •   Annika Backstrom    15 年前
    $s = '10/06/2010 09:10:40 Work not yet started';
    $date = substr($s, 0, 19);
    $msg = substr($s, 20);
    
    $date = strtotime($date);
    // or
    $date = strptime($date, "%m/%d/%Y %H:%M:%S");