4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PascalCase, camelCase, snake_case を一括して揃える

Last updated at Posted at 2014-02-15

正規表現

PascalCase, camelCase, snake_case とかいろいろあるけど一括してどれかに揃えたいなーってときに使える正規表現です。

/[A-Z0-9][^A-Z0-9_]*+|(?:\A|(?<=_))[^A-Z0-9_]++|\A\z/

実装サンプル

クラス定義

class CaseUtil {
 
    public static function pascalize($string) {
        return implode('', array_map('ucfirst', self::parse($string)));
    }
 
    public static function camelize($string) {
        return lcfirst(self::pascalize($string));
    }
 
    public static function snakify($string) {
        return implode('_', array_map('lcfirst', self::parse($string)));
    }
 
    private static function parse($string) {
        preg_match_all(
            '/[A-Z0-9][^A-Z0-9_]*+|(?:\A|(?<=_))[^A-Z0-9_]++|\A\z/',
            $string,
            $matches
        );
        return $matches[0];
    }
 
}

テスト

普段テスト書かないクズプログラマだけどPHPネイティブの assert 関数を初めて使ってみました。PHPUnit導入めんどくさくて

$tests = array(
    'pascalize' => array(
        'SampleMethodTest'          => 'SampleMethodTest',
        'sampleMethodTest'          => 'SampleMethodTest',
        'sample_method_test'        => 'SampleMethodTest',
        'Sample_Method_Test'        => 'SampleMethodTest',
        'SampleMethod_Test'         => 'SampleMethodTest',
        '_sampleMethod_____Test___' => 'SampleMethodTest',
        'AあIいUうEえOお'            => 'AあIいUうEえOお',
        'あいうえお'                 => 'あいうえお',
        '_'                         => '',
        ''                          => '',
    ),
    'camelize' => array(
        'SampleMethodTest'          => 'sampleMethodTest',
        'sampleMethodTest'          => 'sampleMethodTest',
        'sample_method_test'        => 'sampleMethodTest',
        'Sample_Method_Test'        => 'sampleMethodTest',
        'SampleMethod_Test'         => 'sampleMethodTest',
        '_sampleMethod_____Test___' => 'sampleMethodTest',
        'AあIいUうEえOお'            => 'aあIいUうEえOお',
        'あいうえお'                 => 'あいうえお',
        '_'                         => '',
        ''                          => '',
    ),
    'snakify' => array(
        'SampleMethodTest'          => 'sample_method_test',
        'sampleMethodTest'          => 'sample_method_test',
        'sample_method_test'        => 'sample_method_test',
        'Sample_Method_Test'        => 'sample_method_test',
        'SampleMethod_Test'         => 'sample_method_test',
        '_sampleMethod_____Test___' => 'sample_method_test',
        'AあIいUうEえOお'            => 'aあ_iい_uう_eえ_oお',
        'あいうえお'                 => 'あいうえお',
        '_'                         => '',
        ''                          => '',
    ),
);
 
foreach ($tests as $func => $group) {
    foreach ($group as $from => $to) {
        assert("CaseUtil::$func('$from') === '$to'");
    }
}

アサーションに失敗した場所が分かりやすいように変数展開させてみました。失敗しないけど。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?