![]() |
...Another regular expression problem!
I have the following code at the moment:
php Code:
Which outputs: Code:
ArrayCode:
Array |
I've sort of come up with a solution on my own, but it feels a little hackish with all those "OR"s in:
php Code:
Be much easier if I could split by the uppercase letters, but still retain the uppercase letters and include them alongside the matches, as opposed to in a separate array index. |
If I've understood everything correctly, you'll want to take a simpler approach to this. We're using preg_split here in an unconventional manner; usually, one would expect to retrieve repeated items split by a certain character or group of characters:
php Code:
Instead, we're going to be taking a different route grabbing the delimiter itself (in the example above, it would be the whitespace) by using a slightly more complicated regular expression which will result in the split strings (what went into $aWords above) being empty. To return the delimiter matches we can use the flag PREG_SPLIT_DELIM_CAPTURE and to ignore the empty split strings we'll use PREG_SPLIT_NO_EMPTY.To cut a long story short, I think the example below should work for what you need Wildhoney: php Code:
Where it looks like you've been going wrong is that you were only using the \P escape sequence. This means 'any character which is not' whatever follows: \P{Lu} matches any single character which is not an uppercase letter. The alternative is \p does the inverse by matching 'any character which is' whatever follows: \p{Lu} matches a single uppercase letter. |
Thanks :-) I forgot that the upper-case variation means the inverse to the lower-case. Perfect!
|
| All times are GMT. The time now is 10:58 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0