LoginSignup
124
106

More than 5 years have passed since last update.

正規表現でスネークケース↔キャメルケース/パスカルケースの変換

Last updated at Posted at 2014-09-17

前提

ここでは、

  • ローワーキャメルケースキャメルケース
  • アッパーキャメルケースパスカルケース

と呼ぶ。

これは こんな感じ つまり
スネークケース hoge_hoge_hoge 単語をアンダースコア区切りにしたもの
キャメルケース hogeHogeHoge 単語の先頭を大文字にしたもの(最初の単語は先頭が小文字)
パスカルケース HogeHogeHoge 単語の先頭を大文字にしたもの(最初の単語も先頭が大文字)

スネークケース → キャメルケース

hoge_hoge_hoge → hogeHogeHoge

echo 'hoge_hoge_hoge' | sed -r 's/_(.)/\U\1\E/g'

スネークケース → パスカルケース

hoge_hoge_hoge → HogeHogeHoge

echo "hoge_hoge_hoge" | sed -r 's/(^|_)(.)/\U\2\E/g'

キャメルケース → スネークケース

hogeHogeHoge → hoge_hoge_hoge

echo "hogeHogeHoge" | sed -r 's/([A-Z])/_\L\1\E/g'

パスカルケース → スネークケース

HogeHogeHoge → hoge_hoge_hoge

echo "HogeHogeHoge" | sed -r -e 's/^([A-Z])/\L\1\E/' -e 's/([A-Z])/_\L\1\E/g'
124
106
2

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
124
106