-1

I'm trying to get the regex to work that I found in a question here... so I put the script together but I'm getting some syntax errors. please have a look for me.

require 'yaml'

f =  File.open("file.txt")
content = f.read

r = Regex.new(^(\d{13})?$)
ids = content.scan(r).uniq
puts YAML.dump(ids)

This script strips 13 digit numbers from a databump in a text file. Here is the error I'm getting..

ID_Script.rb:7: syntax error, unexpected '^', expecting ')'
r = Regex.new(^(\d{13})?$)
           ^
ID_Script.rb:7: syntax error, unexpected $undefined
r = Regex.new(^(\d{13})?$)

Any help would be appreciated. Thank you

Community
  • 1
  • 1
Dusty Boshoff
  • 1,016
  • 14
  • 39

1 Answers1

3

You have to pass a string to Regex constructor

r = Regex.new("^(\d{13})?$")

or use a regex literal

r = /^(\d{13})?$/
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367