正規表現の基本メタキャラクタ


「◯」は任意の文字または文字列とする。

##.

任意の1文字にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is a cat.'.match(/.... is a cat./)
=> #<MatchData "Maru is a cat.">
[2] pry(main)> 'Maru is a cat.'.match(/.....is a cat./)
=> #<MatchData "Maru is a cat.">
[3] pry(main)> 'Maru is a cat.'.match(/.....is a ..../)
=> #<MatchData "Maru is a cat.">
```

##\w, \W

\w: 英単語を構成する「アルファベット・数字・アンダースコア」1文字にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is a cat.'.match(/\waru is a cat./)
=> #<MatchData "Maru is a cat.">
[2] pry(main)> 'Maru is a cat.'.match(/Maru is a ca\w./)
=> #<MatchData "Maru is a cat.">
[3] pry(main)> 'Maru is a cat.'.match(/Maru is a cat\w/)
=> nil
```

\W: 英単語を構成する「アルファベット・数字・アンダースコア」以外の1文字にマッチするメタキャラクタ

```rb
[1] pry(main)> 'マルは猫です。'.match(/マ\Wは猫です。/)
=> #<MatchData "マルは猫です。">
[2] pry(main)> 'マルは猫です。'.match(/マルは猫です\W/)
=> #<MatchData "マルは猫です。">
[3] pry(main)> 'マルは猫です。'.match(/マルは\Wです。/)
=> #<MatchData "マルは猫です。">
```

##\s, \S


\s: 空白文字にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is at Tokyo.'.match(/Maru\sis at Tokyo./)
=> #<MatchData "Maru is at Tokyo.">
[2] pry(main)> 'Maru is at Tokyo.'.match(/Maru is\sat Tokyo./)
=> #<MatchData "Maru is at Tokyo">
[3] pry(main)> 'Maru is at Tokyo.   '.match(/Maru is at Tokyo.\s\s\s/)
=> #<MatchData "Maru is at Tokyo.   ">
```

\S: 空白文字以外にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is @ Tokyo.'.match(/Maru is \S Tokyo./)
=> #<MatchData "Maru is @ Tokyo.">
[2] pry(main)> 'Maru is @ Tokyo.'.match(/Maru is @ Tokyo\S/)
=> #<MatchData "Maru is @ Tokyo.">
[3] pry(main)> 'Maru is @ 東京.'.match(/Maru is @ \S\S./)
=> #<MatchData "Maru is @ 東京.">
```

##\d, \D

\d: 数字にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is 12 years old.'.match(/Maru is \d2 years old./)
=> #<MatchData "Maru is 12 years old.">
[2] pry(main)> 'Maru is 12 years old.'.match(/Maru is \d\d years old./)
=> #<MatchData "Maru is 12 years old.">
[3] pry(main)> 'Maru is 12 years old.'.match(/Maru is 12\dyears old./)
=> nil
```

\D: 数字以外にマッチするメタキャラクタ

```rb
[74] pry(main)> 'Maru is 12 years old.'.match(/\Daru is 12 years old./)
=> #<MatchData "Maru is 12 years old.">
[75] pry(main)> 'Maru is 12 years old.'.match(/Maru\Dis 12 years old./)
=> #<MatchData "Maru is 12 years old.">
[76] pry(main)> 'Maru is 12 years old.'.match(/Maru is 12 years old\D/)
=> #<MatchData "Maru is 12 years old.">
```

##◯*

0回以上の繰り返しにマッチする表現

```rb
[1] pry(main)> 'Maru is good at climbing trees.'.match(/Maru is go*d at climbing trees./)
=> #<MatchData "Maru is good at climbing trees.">
[2] pry(main)> 'Maru is gooooood at climbing trees.'.match(/Maru is go*d at climbing trees./)
=> #<MatchData "Maru is gooooood at climbing trees.">
[3] pry(main)> 'Maru is gd at climbing trees.'.match(/Maru is go*d at climbing trees./)=> #<MatchData "Maru is gd at climbing trees.">
```

##◯+

1回以上の繰り返しにマッチする表現

```rb
[1] pry(main)> 'Maru is good at climbing trees.'.match(/Maru is go+d at climbing trees./)
=> #<MatchData "Maru is good at climbing trees.">
[2] pry(main)> 'Maru is gooooood at climbing trees.'.match(/Maru is go+d at climbing trees./)
=> #<MatchData "Maru is gooooood at climbing trees.">
[3] pry(main)> 'Maru is gd at climbing trees.'.match(/Maru is go+d at climbing trees./)=> nil
```

##◯{n}

n回の繰り返しにマッチする表現

```rb
[1] pry(main)> 'Maru is good at climbing trees.'.match(/Maru is go{2}d at climbing trees./)
=> #<MatchData "Maru is good at climbing trees.">
[2] pry(main)> 'Maru is god at climbing trees.'.match(/Maru is go{2}d at climbing trees./)
=> nil
[3] pry(main)> 'Maru is goood at climbing trees.'.match(/Maru is go{2}d at climbing trees./)
=> nil
```

##◯{n,}

n回以上の繰り返しにマッチする表現

```rb
[1] pry(main)> 'Maru is good at climbing trees.'.match(/Maru is go{2,}d at climbing trees./)
=> #<MatchData "Maru is good at climbing trees.">
[2] pry(main)> 'Maru is god at climbing trees.'.match(/Maru is go{2,}d at climbing trees./)
=> nil
[3] pry(main)> 'Maru is goood at climbing trees.'.match(/Maru is go{2,}d at climbing trees./)
=> #<MatchData "Maru is goood at climbing trees.">
```

##◯{n,m}

n回以上m回以下の繰り返しにマッチする表現

```rb
[1] pry(main)> 'Maru is good at climbing trees.'.match(/Maru is go{2,3}d at climbing trees./)
=> #<MatchData "Maru is good at climbing trees.">
[2] pry(main)> 'Maru is god at climbing trees.'.match(/Maru is go{2,3}d at climbing trees./)
=> nil
[3] pry(main)> 'Maru is goood at climbing trees.'.match(/Maru is go{2,3}d at climbing trees./)
=> #<MatchData "Maru is goood at climbing trees.">
```

##◯?

0回または1回の出現にマッチする表現

```rb
[1] pry(main)> 'Maru is a Pixie-bob.'.match(/Maru is a Pixie-?bob./)
=> #<MatchData "Maru is a Pixie-bob.">
[2] pry(main)> 'Maru is a Pixiebob.'.match(/Maru is a Pixie-?bob./)
=> #<MatchData "Maru is a Pixiebob.">
[3] pry(main)> 'Maru is a Pixie--bob.'.match(/Maru is a Pixie-?bob./)
=> nil
```

##^

文字列の先頭にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is brave. And Maru is crazy.'.match(/^Maru is brave.*/)
=> #<MatchData "Maru is brave. And Maru is crazy.">
[2] pry(main)> 'Maru is brave. And Maru is crazy.'.match(/^Maru is crazy.*/)
=> nil
```

##$

文字列の末尾にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is a cat. And Pico is a cat.'.match(/.+cat.$/)
=> #<MatchData "Maru is a cat. And Pico is a cat.">
[2] pry(main)> 'Maru is a cat. And Pico is a cat.'.match(/Maru is a cat.$/)
=> nil
```

##\b, \B

\b: 単語の境界にマッチするメタキャラクタ

```rb
[1] pry(main)> 'Maru is a cat.'.match(/.*\ba\b.*/)
=> #<MatchData "Maru is a cat.">
[2] pry(main)> 'Maru is a cat.'.match(/\ba\b/)
=> #<MatchData "a">
```

\B: 単語の境界以外にマッチするメタキャラクタ

```rb
[130] pry(main)> 'Maru is the cat.'.match(/\Ba\B/)
=> #<MatchData "a">
[131] pry(main)> 'Maru is the cat.'.match(/\Bs\b/)
=> #<MatchData "s">
[132] pry(main)> 'Maru is the cat.'.match(/\bth\B/)
=> #<MatchData "th">
```

##[◯-◯], [^◯-◯]

指定された文字のどれかにマッチする表現

```rb
[1] pry(main)> 'Maru is a cat.'.match(/[A-Z]aru is a cat/)
=> #<MatchData "Maru is a cat">
[2] pry(main)> 'Maru is a cat.'.match(/M[^A-Z]ru is a cat/)
=> #<MatchData "Maru is a cat">
[3] pry(main)> 'Maru is a cat.'.match(/M[a-z]ru is a cat/)
=> #<MatchData "Maru is a cat">
[4] pry(main)> 'Maru is a cat.'.match(/[^a-z]aru is a cat/)
=> #<MatchData "Maru is a cat">
```

##[◯◯◯], [^◯◯◯]

マッチする文字の範囲を指定する表現

```rb
[1] pry(main)> 'Maru is a cat.'.match(/[LMN]aru is a cat/)
=> #<MatchData "Maru is a cat">
[2] pry(main)> 'Maru is a cat.'.match(/M[^LMN]ru is a cat/)
=> #<MatchData "Maru is a cat">
[3] pry(main)> 'Maru is a cat.'.match(/M[abc]ru is a cat/)
=> #<MatchData "Maru is a cat">
[4] pry(main)> 'Maru is a cat.'.match(/[^abc]aru is a cat/)
=> #<MatchData "Maru is a cat">
```

##◯◯◯|◯◯◯

指定されたパターンのどれかにマッチする表現

```rb
[1] pry(main)> 'Maru is a cat.'.match(/Maru is a cat.|Pico is a cat./)
=> #<MatchData "Maru is a cat.">
[2] pry(main)> 'Pico is a cat.'.match(/Maru is a cat.|Pico is a cat./)
=> #<MatchData "Pico is a cat.">
[3] pry(main)> 'Maru is a cat.'.match(/(Maru|Pico) is a cat./)
=> #<MatchData "Maru is a cat." 1:"Maru">
[4] pry(main)> 'Pico is a cat.'.match(/(Maru|Pico) is a cat./)
=> #<MatchData "Pico is a cat." 1:"Pico">
```