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?

文字列チェックする際に注意したい正規表現の先頭と末尾

Posted at

単純に正規表現であるパターンの文字列チェックしたいとき

  • 文字の先頭を示す表現は、^ ではなく \A
  • 末尾は、$ ではなく \z

とした方が良いことが多い、という話。

$ は改行(\n)も許してしまうことがあるためだ。

^\Aはそこまでの問題にはならないが、複数行モードで正規表現をかけた場合の挙動が違う。

正規表現なかなか身に付かず、いつもざっとChatGPTに下地をお願いして自分で調べたり、チェックをして修正していたがやたら^$を使いがちだなと感じる。

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?