By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

Detect Upper case characters in a string and separate them with a space

P: n/a
I want to detect uppercase characters in a string and separate the two
words in it by a space. I know I can use preg_replace for it, but I am
not very good at regular expressions. Can someone help?

One example of such a string follows. Please note that there are a lot
of strings like these and I want a generic method to do it, which is
why I think preg_replace would be the perfect solution. If someone can
help me, that would be great!

Example:

$string = "UserLocation";

I want the string to be $string = "User Location";

Zain Ally

Mar 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
zainally wrote:
I want to detect uppercase characters in a string and separate the two
words in it by a space. I know I can use preg_replace for it, but I am
not very good at regular expressions. Can someone help?
You want something like this:

$s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);

You probably want to consider what sort of behaviour is "desirable" in
edge cases like this:

Print404ErrorPage
GetHTTPRequest
Jpeg2Gif

The above regular expression will output:

Print404 Error Page
Get H T T P Request
Jpeg2 Gif

which might not be what you wanted!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 19 '07 #2

P: n/a
On Mar 19, 5:08 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
zainally wrote:
I want to detect uppercase characters in a string and separate the two
words in it by a space. I know I can use preg_replace for it, but I am
not very good at regular expressions. Can someone help?

You want something like this:

$s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);

You probably want to consider what sort of behaviour is "desirable" in
edge cases like this:

Print404ErrorPage
GetHTTPRequest
Jpeg2Gif

The above regular expression will output:

Print404 Error Page
Get H T T P Request
Jpeg2 Gif

which might not be what you wanted!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
That is exactly what I want. Thanks Toby!

Mar 19 '07 #3

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:ms************@ophelia.g5n.co.uk...
zainally wrote:
>I want to detect uppercase characters in a string and separate the two
words in it by a space. I know I can use preg_replace for it, but I am
not very good at regular expressions. Can someone help?

You want something like this:

$s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);

You probably want to consider what sort of behaviour is "desirable" in
edge cases like this:

Print404ErrorPage
GetHTTPRequest
Jpeg2Gif

The above regular expression will output:

Print404 Error Page
Get H T T P Request
Jpeg2 Gif

which might not be what you wanted!
Further to Toby's post:

$TestStr = Print404ErrorPage\nGetHTTPRequest\nJpeg2Gif\nJPEG" ;

$CapRegX = '/(\B[A-Z])(?=[a-z])|(?<=[a-z])([A-Z])/sm';
$RepStr = ' $1$2';
$OutStr = preg_replace($CapRegX,$RepStr,$TestStr);

produces:

Print404 Error Page
Get HTTP Request
Jpeg2 Gif
JPEG

HTH
--

Alan
Mar 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.