LoginSignup
1
0

More than 3 years have passed since last update.

【置換】コメントアウトを削除して改行を残さない方法【正規表現】

Last updated at Posted at 2020-05-30

はじめに

rails newで作成されたファイルにあるデフォルトのコメントアウトをテキストエディタでまとめて削除したい。
でも普通に置換えたら改行が残ってしまう。
そんな時に少し便利な正規表現。

使用環境

  • Visual Studio Code (version 1.45)

やり方

検索する文字列:  ^\s*#.+\n ^[ \t]*#[^\n]*(?:\n|$)
置換え後の文字列: 指定しない(空白)

サンプル

置換前

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module SampleApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

置換後

require_relative 'boot'

require 'rails/all'

Bundler.require(*Rails.groups)

module SampleApp
  class Application < Rails::Application
    config.load_defaults 6.0

  end
end

注意点

使用するテキストエディタによっては挙動が異なるかもしれませんので、異なる環境で使用する際は調整をお願いします。
元々ある空行は基本的に削除しないのですが、コメントアウトの前行が空行の場合、その空行は削除されてしまいます。
※2020.5.31追記 上記の点は解決することができました。ご協力をいただきました@earthdiver1さん、誠にありがとうございます!

1
0
4

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