RegEx and split camelCase

I want to get an array of all capitalized words that are included in a string. But only if the line starts with "set".

For instance:

- string "setUserId", result array("User", "Id") - string "getUserId", result false 

With no restrictions on "set", RegEx looks like /([AZ][az]+)/

+4
source share
1 answer
 $str ='setUserId'; $rep_str = preg_replace('/^set/','',$str); if($str != $rep_str) { $array = preg_split('/(?<=[az])(?=[AZ])/',$rep_str); var_dump($array); } 

Look it up

Your regular expression will also work .:

 $str = 'setUserId'; if(preg_match('/^set/',$str) && preg_match_all('/([AZ][az]*)/',$str,$match)) { var_dump($match[1]); } 

Look it up

+4
source

Source: https://habr.com/ru/post/1380381/


All Articles