0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

正規表現における *、+、*?、+? の違い

Last updated at Posted at 2025-09-27

正規表現の * とか + とか、もう忘れすぎ問題

正規表現の * とか + とか、気づけば何百回とGoogle検索してる気がするので、いい加減ここに自分用メモとしてまとめておきます。

記号 意味 マッチ回数 デフォルト動作
* 0回以上 0,1,2,… 最長マッチ(Greedy)
+ 1回以上 1,2,… 最長マッチ(Greedy)
*? 0回以上 0,1,2,… 最短マッチ(Lazy)
+? 1回以上 1,2,… 最短マッチ(Lazy)

こういう「直前の文字やグループが何回繰り返されるか」を指定する記号のことを 量指定子(Quantifier) と呼びます。 *+ のほかに、{n,m} みたいなのも量指定子の仲間です。

覚え方の小ネタ(AIに考えさせてみた)

  • * → 「ゼロから無限まで」 → スターは無限の可能性
  • + → 「最低1回」 → プラスはポジティブ、必ず1回以上
  • ? がつくと → 「ちょっと遠慮がち」になる

…まあ、これで少しは頭に残りやすくなるかも?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?