Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

php - Regex - Split string at every datetime

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

See the 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);

Output:

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
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...