0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VS Codeで正規表現の検索をしたときに調べたこと

Posted at

最初の試み

Railsシステムで任意のエラークラスを検索しようとした時、最初に以下のような検索パターンを試みましたが、期待した結果が得られませんでした:

class *Error

これが機能しなかった理由は、正規表現では*単体では「任意の文字列」を表現できないためです。*は量指定子(quantifier)であり、直前のパターンの繰り返し回数を指定するものです。

正しい正規表現

正しい正規表現パターンは以下になります:

class.*Error

正規表現の解説

この正規表現は以下の要素で構成されています:

  1. class - リテラルで"class"という文字列にマッチ
  2. . - 改行を除く任意の1文字にマッチ
  3. * - 直前のパターン(この場合は.)が0回以上繰り返されることを示す量指定子
  4. Error - リテラルで"Error"という文字列にマッチ

したがって、このパターンは「"class"で始まり、その後に任意の文字列(0文字以上)が続き、最後に"Error"で終わる」という文字列を検索します。

マッチする例

以下のような文字列にマッチします:

  • class StandardError
  • class CustomError
  • class MyApplicationError
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?