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

More than 1 year has passed since last update.

【Perl初学者】よく使う正規表現一覧

Posted at

目次

パターンマッチ

意味
   =~    文字列と正規表現を比較する際に使う 例 Takopa =~ /^T\w{5}$/
   ( )    対象の文字列と()の中身がマッチした場合に,()の中身を取得する(グループ化)
   / /    対象の文字列を/から/までの正規表現で比較する

置換

意味
 s  文字列を置換 例 s/パターン/置換文字列/
$1 グループ化した部分を$1に格納できる

正規表現文字

意味
 \d  0~9の文字
 \D  0~9以外の文字
 \w  a~z,A~Z,0~9,_(アンダーバー)
 \W  a~z,A~Z,0~9,_以外
\s スペース, tab文字, 改行などの空白文字
 \S  スペース, tab文字, 改行以外
  .   改行を除くすべての文字

文字列のクラス

意味
 [ ]   []内のどれか一つにマッチした場合 例) p1, q1, r1 -> [pqr]1 #どれも当てはまる

正規表現のオプション

1 2
  s   改行を.にマッチさせる
g マッチしたすべての文字列を置換する

まとめ

正規表現は覚えることが多く現場で使われているのは基本複雑な書き方になっているので、1つ1つの意味を読み解いていくのが大事だと思う。

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