0
$input = "some words go here priority: p1,p2 -rank:3 status: not delayed";

$pattern = "/(\S+):\s*(.*?)(?=\S+:|$)|(.*?)(?=\S+:|$)/";

preg_match_all($pattern, $input, $matches);

Example: http://regex101.com/r/yM0wO1#pcre

The above pattern ends up outputting an extra empty array at the end. (See Match 5 in the example)
Everything else is the way I expect it to be...

How can I prevent the extra empty array?

EDIT: BACKGROUND INFO

I have data formatted as such:

some words go here priority: p1,p2 -rank:3 status: not delayed

Basically I need to retrieve each set of data that corresponds to the colon name.

Ideally if I could end up with an array structure such that

'' => 'some words go here'
priority => 'p1,p2'
-rank    => 3
status   => 'not delayed'

A few caveats:

keywords will not have a defining colon-word (keywords are just placed in the front)

keywords will not always exist (might just be colon-words)

colon-words will not always exist (might just be keywords)

2 Answers 2

1

A better way would be to split instead of matching it.

(?=\s\S+:)

Each string would contain the key-value pair or only value if there's no key

Sign up to request clarification or add additional context in comments.

2 Comments

Well, not quite... I've added some background info to hopefully make clearer what I need.
@kylex split it instead of matching it with above regex.
0

try this

(\S+):\s*(.*?)(?=\S+:|$)|(.*?)(?=\S+:)

1 Comment

Added some needed background info which explains the caveats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.