将毫秒设为可选?
/^([\d]{2})-([\d]{2})-([\d]{2}|[\d]{4})\s+([\d]{2}):([\d]{2}):([\d]{2})\.?(\d+)?\s+(.*?)$/
例子:
<?php
$strings = <<< LOL
10-10-15 15:16:41.1 Some Text here
10-10-15 15:16:41.12 Some Text here
10-10-15 15:16:41.123 Some Text here
10-10-15 15:16:41 Some Text here
LOL;
preg_match_all('/^([\d]{2})-([\d]{2})-([\d]{2}|[\d]{4})\s+([\d]{2}):([\d]{2}):([\d]{2})\.?(\d+)?\s+(.*?)$/m', $strings , $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$day = $matches[1][$i];
$month = $matches[2][$i];
$year = $matches[3][$i];
$hours = $matches[4][$i];
$minutes = $matches[5][$i];
$seconds = $matches[6][$i];
$ms = $matches[7][$i];
$text = $matches[8][$i];
echo "$day $month $year $hours $minutes $seconds $ms $text \n";
}
正则表达式演示:
https://regex101.com/r/aF9wN6/1
PHP 演示:
http://ideone.com/1aEt2E
正则表达式解释:
^([\d]{2})-([\d]{2})-([\d]{2}|[\d]{4})\s+([\d]{2}):([\d]{2}):([\d]{2})\.?(\d+)?\s+(.*?)$
Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^»
Match the regex below and capture its match into backreference number 1 «([\d]{2})»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{2}»
Exactly 2 times «{2}»
Match the character “-” literally «-»
Match the regex below and capture its match into backreference number 2 «([\d]{2})»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{2}»
Exactly 2 times «{2}»
Match the character “-” literally «-»
Match the regex below and capture its match into backreference number 3 «([\d]{2}|[\d]{4})»
Match this alternative (attempting the next alternative only if this one fails) «[\d]{2}»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{2}»
Exactly 2 times «{2}»
Or match this alternative (the entire group fails if this one fails to match) «[\d]{4}»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{4}»
Exactly 4 times «{4}»
Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, form feed) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regex below and capture its match into backreference number 4 «([\d]{2})»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{2}»
Exactly 2 times «{2}»
Match the character “:” literally «:»
Match the regex below and capture its match into backreference number 5 «([\d]{2})»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{2}»
Exactly 2 times «{2}»
Match the character “:” literally «:»
Match the regex below and capture its match into backreference number 6 «([\d]{2})»
Match a single character that is a “digit” (any decimal number in any Unicode script) «[\d]{2}»
Exactly 2 times «{2}»
Match the character “.” literally «\.?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the regex below and capture its match into backreference number 7 «(\d+)?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character that is a “digit” (any decimal number in any Unicode script) «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, form feed) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regex below and capture its match into backreference number 8 «(.*?)»
Match any single character that is NOT a line break character (line feed) «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»