5-3-4 先頭と末尾

In [2]:
pattern = /\A\d{3}-\d{4}-\d{4}\z/

pattern === '080-1234-5678'
Out[2]:
true
In [3]:
pattern === 'Phone: 080-1234-5678'
Out[3]:
false
In [4]:
pattern === '080-1234-5678 (mobile)'
Out[4]:
false
In [5]:
lines = "1234\nabcd"

p(/\A\d+\z/ === lines)
/^\d+$/ === lines
false
Out[5]:
true

5-3-5 グルーピングと後方参照/部分式呼び出し

In [6]:
p(/(B)\ to\ \1/ === 'B to B')

$1
true
Out[6]:
"B"
In [7]:
/(?<number>[0-9]+)/ === 'abc-123'

Regexp.last_match[:number]
Out[7]:
"123"
In [8]:
/(?<num>[0-9]+)[a-c\-]+\k<num>/ === '123-abc-123'
Out[8]:
true
In [13]:
/(?<num>[0-9]+)[a-c\-]+\k<num>/ === '123-abc-123'
Out[13]:
#<MatchData "123-abc-123" num:"123">
In [23]:
/(?<num>[0-9]+)(?<alp>[a-c\-]+)\k<num>/ === '123-abc-123'
Regexp.last_match.named_captures
# 2.4で来る
NoMethodError: undefined method `named_captures' for #<MatchData "123-abc-123" num:"123" alp:"-abc-">
(pry):41:in `<main>'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:242:in `catch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:241:in `catch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:241:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/backend.rb:65:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/backend.rb:12:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:87:in `execute_request'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:47:in `dispatch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:37:in `run'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/command.rb:70:in `run_kernel'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/command.rb:34:in `run'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/bin/iruby:5:in `<top (required)>'
/Users/ariga/.rbenv/versions/2.3.0/bin/iruby:23:in `load'
/Users/ariga/.rbenv/versions/2.3.0/bin/iruby:23:in `<main>'
In [26]:
phone = '080-1234-5678'

p(/([0-9]+)-\g<1>-\g<1>/ === phone)

p(/([0-9]+)-\1-\1/ === phone)
/([0-9]+)-\1-\1/ === '080-080-080'
true
false
Out[26]:
true
In [28]:
/(?<num>[0-9]+)-\g<num>-\g<num>/ === phone
Out[28]:
true