Alina
Alina

Reputation: 2261

Ruby, each line in a file, match regular expression

I do not understand why this code doesnt find the pattern in the string. I open a file and read each line in the file and split it with "/t" and then I try to find a pattern. But it doesnot find it. input file:

1553338_at/C1orf55  225142_at/JHDM1D    0.9075880395022964  
1553338_at/C1orf55  230778_at/---   0.9133682114964662

code:

input=File.open("/lalal/lalal.txt","r")

input.each{|line|

    keys=line.split("\t")
    puts(keys[1])

    a=/---/.match(keys[1])
    puts(a.class)    

}

output:

225142_at/JHDM1D
NilClass
230778_at/---
NilClass

I do not understand why it cannot find "---" in 230778_at/--- ? Thanks in advance

Upvotes: 1

Views: 1015

Answers (2)

sawa
sawa

Reputation: 168101

It does match (on Ruby 2.0.0).

/---/.match("230778_at/---")
# => #<MatchData "---">

Upvotes: 0

mcfinnigan
mcfinnigan

Reputation: 11638

- 

is a regexp reserved symbol. Use

\-

Even better, change your match line to

a = /\-{3}/.match(keys[1])

Upvotes: 1

Related Questions