简体   繁体   中英

Combine regular expressions for splitting camelCase string into words

I managed to implement a function that converts camel case to words, by using the solution suggested by @ridgerunner in this question:

Split camelCase word into words with php preg_match (Regular Expression)

However, I want to also handle embedded abreviations like this:

'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded'

I came up with this solution:

    <?php 

    function camelCaseToWords($camelCaseStr)
    {

        // Convert: "TestASAPTestMore" to "TestASAP TestMore"

        $abreviationsPattern = '/' . // Match position between UPPERCASE "words"
            '(?<=[A-Z])' . // Position is after group of uppercase,
            '(?=[A-Z][a-z])' . // and before group of lowercase letters, except the last upper case letter in the group.
            '/x';
        $arr = preg_split($abreviationsPattern, $camelCaseStr);
        $str = implode(' ', $arr);

        // Convert "TestASAP TestMore" to "Test ASAP Test More"
        $camelCasePattern = '/' . // Match position between camelCase "words".
            '(?<=[a-z])' . // Position is after a lowercase,
            '(?=[A-Z])' . // and before an uppercase letter.
            '/x';

        $arr = preg_split($camelCasePattern, $str);
        $str = implode(' ', $arr);

        $str = ucfirst(trim($str));
        return $str;
    }

    $inputs = array(
    'oneTwoThreeFour',
    'StartsWithCap',
    'hasConsecutiveCAPS',
    'ALLCAPS',
    'ALL_CAPS_AND_UNDERSCORES',
    'hasABREVIATIONEmbedded',
    );

    echo "INPUT";

    foreach($inputs as $val) {
        echo "'" . $val . "' translates to '" . camelCaseToWords($val). "'\n";
    }

The output is:

    INPUT'oneTwoThreeFour' translates to 'One Two Three Four'
    'StartsWithCap' translates to 'Starts With Cap'
    'hasConsecutiveCAPS' translates to 'Has Consecutive CAPS'
    'ALLCAPS' translates to 'ALLCAPS'
    'ALL_CAPS_AND_UNDERSCORES' translates to 'ALL_CAPS_AND_UNDERSCORES'
    'hasABREVIATIONEmbedded' translates to 'Has ABREVIATION Embedded'

It works as intended.

My question is: Can I combine the 2 regular expressions $abreviationsPattern and camelCasePattern so i can avoid running the preg_split() function twice?

These are always fun puzzles to solve; I've narrowed down the cases to just two:

  1. Detect words that start with a capital followed by lowercase letters (but not preceded by a word boundary or start of the subject) - (?<!\\b)[AZ][az]+

  2. Detect transitions from lowercase to uppercase - (?<=[az])[AZ]

     function camelFix($str) { return preg_replace_callback('/(?<!\\b)[AZ][az]+|(?<=[az])[AZ]/', function($match) { return ' '. $match[0]; }, $str); } 

It works for the inputs you have given; it might fail cases that I have not foreseen :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM