tkymtk's blog

Ruby on Rails及びその周辺について調べたこと。Do whatever you want to do at your own pace.

Sublime Text 正規表現で複数行にマッチさせる。

正規表現

  • (?s)フラグを使う(.が改行にもマッチするようになる)
  • [\s\S]*?を使う

  • class … endにマッチしてマッチしたものを取得
(?s)^(class.*?end)
^(class(?s).*?end)
^(class[\s\S]*?end)
class … endを取得して```ruby … end```で囲む

マッチしたいコード

class Comments < ActiveRecord::Base
  belongs_to :post
  has_one :guest
end 

class Guest < ActiveRecord::Base
  belongs_to :comment
end

Find What:

(?s)^(class.*?end) 

Replace With:

```ruby\n$1\n```

結果

 ```ruby
 class Comments < ActiveRecord::Base
   belongs_to :post
   has_one :guest
 end 
 ```

 ```ruby
 class Guest < ActiveRecord::Base
   belongs_to :comment
 end
 ```

資料

Element Standardized Effect when set
mod_s No Normally whether Boost.Regex will match "." against a newline character is determined by the match flag match_dot_not_newline. Specifying this flag is equivalent to prefixing the expression with (?s) and therefore causes "." to match a newline character regardless of whether match_not_dot_newline is set in the match flags.

リンク

間違いがあれば、ご指摘下さると幸いです。