LoginSignup
2
0

More than 1 year has passed since last update.

Juliaで文字列をスネークケースに変換する

Posted at
function snakecase(str)
     str = replace(str, r"(?<a>[a-z])(?<b>[A-Z])" => s"\g<a>_\g<b>")
     str = replace(str, "-" => "_")
     str = replace(str, r"\s+" => "_")
     return lowercase(str)
end
    
snakecase("FOOBAR")  # => foobar
snakecase("FOO-BAR") # => foo_bar
snakecase("FooBAR")  # => foo_bar
snakecase("foo Bar") # => foo_bar

replace 関数で置換する際は、(?<name>) でキャプチャして、\g<name> で展開する。

2
0
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
2
0