Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Rubyの正規表現まとめ

More than 3 years have passed since last update.

https://docs.ruby-lang.org/ja/latest/doc/spec=2fregexp.html

\w 単語構成文字 [a-zA-Z0-9_]
\W 非単語構成文字 [^a-zA-Z0-9_]
\s 空白文字 [ \t\r\n\f\v]
\S 非空白文字 [^ \t\r\n\f\v]
\d 10進数字 [0-9]
\D 非10進数字 [^0-9]
\h 16進数字 [0-9a-fA-F]
\H 非16進数字 [^0-9a-fA-F]

回数

* 0回以上
+ 1回以上
? 0回もしくは1回
{n} ちょうどn回(nは数字)
{n,} n回以上(nは数字)
{,m} m回以下(mは数字)
{n,m} n回以上m回以下(n,mは数字)

位置

^ 行頭(文字列の先頭もしくは改行の次)
$ 行末(文字列の末尾もしくは改行の手前)
\A 文字列の先頭
\Z 文字列の末尾
\z 文字列の末尾
\b 単語を成す文字と単語を成さない文字の間
\B 非単語境界

method

match
MatchDataオブジェクトを返しマッチしなかったときはnilを返します

sub,gsub
マッチした部分を置換

sunoko
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away