Fallion
Fallion

Reputation: 79

Ruby: Split: Premature end of regular expression


I am having problems using the split function in Ruby.
/Users/simonprochazka/Downloads/pes_test_p00/lib/main.rb:28: premature end of regular expression: /(/

 File.open(ARGV[0], "r") do |f|
   f.each_line do |line|
     data = line.split(/\t/)
     puts data[4]
     if data[4] =~ ["("]
     special = data[4].split(/(/)
     scores = special[0].split(/:/)
     puts data[4]
     else
     scores = data[4].split(/:/)
     end
     if special[1] != nil
     matches << Match.new(data[0], scores[0], scores[1], special[1].chop)
     else
     matches << Match.new(data[0], scores[0], scores[1])
     end
 end
 end

Upvotes: 1

Views: 626

Answers (2)

Nakilon
Nakilon

Reputation: 35074

Like said Nikita, ( is a part of regex syntax, so this character have to be escaped by \.
Better use string instead of regex as parameter, when you want to split by only one character. Use split('\\'), split(':') etc.
Note, that string also have special characters and \' works as escaping of ', so you have to double it.

Upvotes: 2

Nikita Rybak
Nikita Rybak

Reputation: 68006

If you want to use ( character in regex, escape it: /\(/. Otherwise, it'll open a group. And unclosed group causes failure.

Upvotes: 4

Related Questions