0

I have a string like this:

Name: John Doe

Age: 23

Primary Language: English

Description: This is a multiline
description field that I want 
to capture

Country: Canada

That's not the actual data, but you can see what I'm trying to do. I want to use regex to get an array of the "key" fields (Name, Age, Primary Language, Description, Country) and their values.

I'm using PHP.

My current attempt is this, but it doesn't work:

preg_match( '/^(.*?\:) (.*?)(\n.*?\:)/ism', $text, $matches );
2
  • There is a \n but your modifiers say that every newline is a new string. And the . also matches \n Commented May 11, 2012 at 13:48
  • I assume the data is populated by a user. Is the data coming from a $_POST or out of a database? Commented May 11, 2012 at 20:37

2 Answers 2

1

Here's one solution: http://rubular.com/r/uDgXcIvhac.

    \s*([^:]+?)\s*:\s*(.*(?:\s*(?!.*:).*)*)\s*

Note that I've used a negative lookahead assertion, (?!.*:). This is the only way you can check that the next line doesn't look like a new field, and at the same time continue where you left off. (This is why lookaheads and lookbehinds are known as zero-width assertions.)

EDIT: Removed bit about arbitrary-width lookaheads; I was mistaken. The above solution is fine.

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

Comments

0

Would PHP's strtok help you? You could use it with ":" as the delimeter/token and trim leading and trailing spaces to remove the unwanted new lines.

http://php.net/manual/en/function.strtok.php

Comments

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.