On Wed, August 9, 2006 1:30 am, Dave M G wrote:
> This regular expression stuff is way tricky.
>
> Thanks to help from this list, I have an expression that will select
> the
> first word of a string, up to the first white space:
> "#^(.*)\s#iU"
>
> But after some consideration, I realized that I wanted to keep both
> parts of the original text. The first word, and then everything that
> came after it, should be divided and stored in separate variables.

$pattern = "#^([^\\s]*)(\\s*)(.*\$)#sU";
preg_match($pattern, $input, $parts);
echo "Before whitespace: $parts[1]<br />\n";
echo "Whitespace: $parts[2]<br />\n";
echo "After whitespace: $parts[3]<br />\n";

De-construction:
#^ Anchored at the beginning of the string
([^\\s]*) Capture everything that's not whitespace
(\\s*) Capture a contiguous run of whitespace
(.*\$) Capture anything at all to the end of the string
#sU 's' is mult-line, and U is ungreedy

Aha!

You may need to use (.*\$)? to get the greedy back "on" for the ending
bit, so that \$ does not match the FIRST newline, but matches the END
of the string.

Please find and download "The Regex Coach" and play around with it.

Its real-time syntax/pattern/match highlighting makes Regex a hell of
a lot easier to figure out.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to