正则表达式 - 在每个日期时间拆分字符串

时间:2015-08-14 15:10:21

标签: php regex

我正在尝试将每次有时间戳时从Feed中获取的更新字符串拆分为数组。

这是我到目前为止的正则表达式,但它似乎只找到字符串中的第一个日期时间。

^(\d{1,2}\/\d{1,2}\/\d{4})

以下是我的字符串示例。

$Comment = "8/13/2015 11:44:10 AMVN - Upon additional underwriting review. Account will be declined due to inconsistencies in personal and/or business information that can not be verified or validated 8/13/2015 8:32:52 AMFA Rcvd Change In Terms letter, will fwd to the Underwriter. 8/10/2015 1:21:17 PMVN - Please provide change in term letter capping monthly volume $20K, average ticket to $500 and high ticket to $1K. 8/10/2015 11:02:19 AMVN Declined as the financial condition do not support business type and requested limits. 8/10/2015 9:37:03 AMFA Rcvd Bank Statements, will fwd to the Underwriter. 8/4/2015 3:35:05 PMVN - Please provide 3 most recent bank statements and 3 most recent processing statements. 8/4/2015 9:52:04 AMBAI In Underwriting iEntry Application";

使用这个例子,我希望有一个包含七个值的数组。

$Pattern = "^(\d{1,2}\/\d{1,2}\/\d{4})";
$Comments = preg_split($Pattern, $Comment);

2 个答案:

答案 0 :(得分:2)

不要将它锚定到字符串的开头,因此摆脱^

(\d{1,2}\/\d{1,2}\/\d{4})

答案 1 :(得分:1)

当您需要在日期字符串处分割没有换行符的长字符串时,可以考虑使用

regex split 方法
\s+(?=<DATE_PATTERN HERE>)              # DATE is preceded with whitespace, anything can follow
\s+(?=<DATE_PATTERN HERE>\b)            # DATE is preceded with whitespace, date is not followed with letter/digit/_
\s*(?<!\d)(?=<DATE_PATTERN HERE>)       # Whitespace before date optional, no digit before
\s*(?<!\d)(?=<DATE_PATTERN HERE>)(?!\d) # Whitespace before date optional, no digit before and after
\s*(?<!\d)(?<!\d<DEL>)(?=<DATE_PATTERN HERE>)(?!<DEL>?\d) # Whitespace before date optional, no digit with delimiter before and after

在这里,您可以使用一个简单的\s+(?=\d{1,2}/\d{1,2}/\d{4})正则表达式,它与1+空格后跟匹配((?=...)是一个积极的解决方法,它不会消耗任何文本,只需检查一下如果存在匹配项并返回true或false,则一位或两位数/,一位或两位数/和一位四位

$records = preg_split('~\s+(?=\d{1,2}/\d{1,2}/\d{4})~', $Comment);

请参见PHP demo

$Comment = "8/13/2015 11:44:10 AMVN - Upon additional underwriting review. Account will be declined due to inconsistencies in personal and/or business information that can not be verified or validated 8/13/2015 8:32:52 AMFA Rcvd Change In Terms letter, will fwd to the Underwriter. 8/10/2015 1:21:17 PMVN - Please provide change in term letter capping monthly volume $20K, average ticket to $500 and high ticket to $1K. 8/10/2015 11:02:19 AMVN Declined as the financial condition do not support business type and requested limits. 8/10/2015 9:37:03 AMFA Rcvd Bank Statements, will fwd to the Underwriter. 8/4/2015 3:35:05 PMVN - Please provide 3 most recent bank statements and 3 most recent processing statements. 8/4/2015 9:52:04 AMBAI In Underwriting iEntry Application";
$records = preg_split('~\s+(?=\d{1,2}/\d{1,2}/\d{4})~', $Comment);
print_r($records);

输出:

Array
(
    [0] => 8/13/2015 11:44:10 AMVN - Upon additional underwriting review. Account will be declined due to inconsistencies in personal and/or business information that can not be verified or validated
    [1] => 8/13/2015 8:32:52 AMFA Rcvd Change In Terms letter, will fwd to the Underwriter.
    [2] => 8/10/2015 1:21:17 PMVN - Please provide change in term letter capping monthly volume $20K, average ticket to $500 and high ticket to $1K.
    [3] => 8/10/2015 11:02:19 AMVN Declined as the financial condition do not support business type and requested limits.
    [4] => 8/10/2015 9:37:03 AMFA Rcvd Bank Statements, will fwd to the Underwriter.
    [5] => 8/4/2015 3:35:05 PMVN - Please provide 3 most recent bank statements and 3 most recent processing statements.
    [6] => 8/4/2015 9:52:04 AMBAI In Underwriting iEntry Application
)