Regex_named_capture

A named capture is defined like this: ?<name>

If you want to use =~ in Ruby - and use named capture - you need to put the regex expression to the left.

Example:

data = 'Arne: 24'

if /^(?<name>\w+):\s+(?<age>\d+)$/ =~ data
  # you have access to two new variables:
  #   name & age
  puts 'name: %s, age: %s' % [name, age]
end

# THIS WILL NOT WORK:
# data =~ /^(?<name>\w+):\s+(?<age>\d+)$/

find out.. Link to heading

What if the variable is a global? Or a constant?