14

I want to add a space to a string on capital letters using a PHP method like preg_replace() and a regex, but I only want to add a space on the first capital letter when they are continuous. I also would like the regex to know that the last capital in a continuous string of capitals should be the next capital to put a space before.

These strings are examples:

TodayILiveInTheUSAWithSimon
USAToday
IAmSOOOBored

become:

 Today I Live In The USA With Simon
 USA Today
 I Am SOOO Bored

Can this be done, and how?

This question ( Regular expression, split string by capital letter but ignore TLA ), seems to accomplish this with .net.

WORKING SOLUTION

Here is the complete code that I use:

$string = 'TodayILiveInTheUSAWithSimon';
$regex = '/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/';
$string = preg_replace( $regex, ' $1', $string );

Both of these regex's work:

/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/
/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/

The first one is from @Regexident's solution below, and is very very slightly faster than the second one.

Community
  • 1
  • 1
T. Brian Jones
  • 13,002
  • 25
  • 78
  • 117

1 Answers1

12

Find:

(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))

Replace:

 $1

note the space before $1

Edit: fix.

Regexident
  • 29,441
  • 10
  • 93
  • 100
  • What PHP methods should I use to accomplish this with this regex? – T. Brian Jones Dec 23 '11 at 02:30
  • @T.BrianJones: That would be [preg_replace](http://php.net/manual/en/function.preg-replace.php). – Regexident Dec 23 '11 at 02:35
  • I'm getting the following error: `preg_replace(): Compilation failed: POSIX named classes are supported only within a class at offset 4` when I execute this: `echo preg_replace( '((?<![:upper:]|^)[:upper:]|(?<!^)[:upper:](?![:upper:]))', ' $1', $string );` – T. Brian Jones Dec 23 '11 at 02:40
  • 3
    Please see fixed answer. I forgot that PHP requires double brackets around `:upper:`, my bad. My PHP is a bit dusty, unlike my regex. :P – Regexident Dec 23 '11 at 02:45