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. |
リンク
- Options for Perl Regular Expressions - 1.47.0
- sublimetext2 - RegEx in Sublime Text: Match any character, including newlines? - Stack Overflow
- 複数行にマッチさせる正規表現 | You Look Too Cool
- 二行以上に対するマッチングでトラブルがありました。何が悪いのでしょう? - Perl の正規表現のFAQやTIPS等
- .*? ドット スター クエスチョンマーク あるいは ドット アスタリスク クエスチョンマーク(はてな)とはなにか - tkymtk's blog