Regex To Break Up camelCase String PHP

Let's say I have the following line:

getPasswordLastChangedDatetime 

How could I break this in capital letters so that I can get:

 get Password Last Changed Datetime 
+3
string php regex
Jul 04 2018-11-11T00:
source share
7 answers

If you like ASCII characters the most:

 $parts = preg_split("/(?=[AZ])/", $str); 

Demo

The construct (?= ..) is called lookahead [docs] .

This works if the parts only contain a capital symbol at the beginning. It gets more complicated if you have things like getHTMLString . This can be compared to:

 $parts = preg_split("/((?<=[az])(?=[AZ])|(?=[AZ][az]))/", $str); 

Demo

+6
Jul 04 2018-11-11T00:
source share

Ask about it too early, found this:

 preg_replace('/(?!^)[[:upper:]]/',' \0',$test); 
+1
Jul 04 2018-11-11T00:
source share

For example:

 (?:^|\p{Lu})\P{Lu}* 
+1
Jul 04 2018-11-11T00:
source share

No complicated solution needed. It does

 preg_replace('/([AZ])/',"\n".'$1',$string); 

It certainly doesn't care about acronyms

0
Jul 04 2018-11-11T00:
source share
 preg_split('@(?=[AZ])@', 'asAs') 
0
Jul 04 2018-11-11T00:
source share

Use this: [az]+|[AZ][az]* or \p{Ll}+|\p{Lu}\p{Ll}*

0
Jul 04 2018-11-11T00:
source share
  preg_split("/(?<=[az])(?=[AZ])/",$password)); 
0
Jul 04 2018-11-11T00:
source share