正则表达式-在每个日期时间分割字符串 [英] Regex - Split string at every datetime

查看:699
本文介绍了正则表达式-在每个日期时间分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当有时间戳记时,我都会尝试将从提要中获取的更新字符串拆分为一个数组.

I'm trying to split up a update string I get from a feed into an array each time there is a time stamp.

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

This is the regex I have so far, but it seems to only find the first datetime in the string.

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

这是我的字符串的一个例子.

Here is an example of my string.

$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";

使用此示例,我想有一个包含七个值的数组.

Using this example, I would like to have an array with seven values.

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

推荐答案

当您需要分割一个长字符串且在日期字符串处没有换行符时,您可以考虑使用 regex split 方法和

When you need to split a long string with no line breaks at a date string, you may consider a regex split method with

\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)一位或两位数,/,一位或两位数,/和四位:

Here, you may use a simple \s+(?=\d{1,2}/\d{1,2}/\d{4}) regex that matches 1+ whitespaces followed with (a (?=...) is a positive lookaround that does not consume any text, just checks if there is a match and returns true or false) one or two digits, /, one or two digits, / and four digits:

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

请参见 PHP演示:

$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
)

这篇关于正则表达式-在每个日期时间分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆