Use negated character class (see on rubular.com):
/^([^:]*):(.*)$/m
The […]
is a character class. Something like [aeiou]
matches one of any of the lowercase vowels. [^…]
is a negated character class. [^aeiou]
matches one of anything but the lowercase vowels.
The ^
and $
at the beginning and end of the pattern are the beginning and end of the line anchors. The m
modifiers turns on the multi-line mode.
The problem with your original pattern is that you're (ab)using .
when you could've been a lot more specific, and since *
is greedy, the first group overmatched. It's tempting to try to "fix" that by making the repetition reluctant, but it's MUCH better to be more specific and say that the first group is matching anything but :
.
Note however that this is a matching pattern, with captures. It's not actually a splitting pattern that matches only the delimiter. The delimiter pattern really is just :
.
Related questions
PHP snippet
Given this:
$text = <<<EOT
Company Name: Name of the company, place.
Company Address: Some, address, here.
Link: http://www.somelink.com
EOT;
preg_match_all('/^([^:]*):(.*)$/m', $text, $matches, PREG_SET_ORDER);
print_r($matches);
The output is (as seen on ideone.com):
Array
(
[0] => Array
(
[0] => Company Name: Name of the company, place.
[1] => Company Name
[2] => Name of the company, place.
)
[1] => Array
(
[0] => Company Address: Some, address, here.
[1] => Company Address
[2] => Some, address, here.
)
[2] => Array
(
[0] => Link: http://www.somelink.com
[1] => Link
[2] => http://www.somelink.com
)
)