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?

More than 5 years have passed since last update.

[a-z]と^[a-z]$の違い

Posted at

##[a-z]と^[a-z]$の違い

[a-z]と^[a-z]$の違いについて書いてみる。

^, $の意味は以下の通り。

| 記号 | 意味 |
|:-----------------|------------------:|:------------------:|
| ^ | 行頭 | This |
| $ | 行末 | column |

[a-z]に^, $を加えると、どう変化するかコードで試してみよう。

sample.rb
p "ruby" =~ /[a-z]/
# => 0

p "ruby" =~ /^[a-z]$/
# => nil 

^[a-z]$を使うと、nilが出力されてしまう。しかし、

sample.rb
p "a" =~ /^[a-z]$/
# => 0

^, $をつけることで一文字だけの検証を行うことができる。

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?