Combine regular expressions for splitting camelCase string into words

谁说我不能喝 提交于 2020-01-14 10:36:08

问题


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?


回答1:


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)[A-Z][a-z]+

  2. Detect transitions from lowercase to uppercase - (?<=[a-z])[A-Z]

    function camelFix($str)
    {
        return preg_replace_callback('/(?<!\b)[A-Z][a-z]+|(?<=[a-z])[A-Z]/', function($match) {
            return ' '. $match[0];
        }, $str);
    }
    

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



来源:https://stackoverflow.com/questions/17361768/combine-regular-expressions-for-splitting-camelcase-string-into-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!