I absolutely cannot figure out why this does not find a match! I tried
searching this forum before I posted… I’ve been stuck for hours on
such a simple problem… I feel retarded.
lines is an array of strings:
def go(lines)
reg=/[trace-(\d+)]/
lines.each do |line|
puts line.slice!(0,25) #speed... some lines are long
puts reg.match(line)[1]
end
end
Here is an example string:
[trace-2932] Application=3071000
I get this:
undefined method `[]’ for nil:NilClass (NoMethodError)
on the reg.match[1] line.
I absolutely cannot figure out why this does not find a match! I tried
searching this forum before I posted… I’ve been stuck for hours on
such a simple problem… I feel retarded.
$ ri -T String#slice!
---------------------------------------------------------- String#slice!
str.slice!(fixnum) => fixnum or nil
str.slice!(fixnum, fixnum) => new_str or nil
str.slice!(range) => new_str or nil
str.slice!(regexp) => new_str or nil
str.slice!(other_str) => new_str or nil
Deletes the specified portion from _str_, and returns the portion
deleted. The forms that take a +Fixnum+ will raise an +IndexError+
if the value is out of range; the +Range+ form will raise a
+RangeError+, and the +Regexp+ and +String+ forms will silently
ignore the assignment.
string = "this is a string"
string.slice!(2) #=> 105
string.slice!(3..6) #=> " is "
string.slice!(/s.*t/) #=> "sa st"
string.slice!("r") #=> "r"
string #=> "thing"
if the value is out of range; the +Range+ form will raise a
Hope that helps.
James Edward G. II
I would also recommend anchoring your regexp. In fact, you will probably
get
better performance by anchoring your RE and removing the splice (unless
the
lines are a LOT longer than your example line). If you anchor your
regexp to
the beginning of the line, the engine will stop trying to match as soon
as it
cannot match the [trace-. To do this, start the regexp with a ^ i.e.
/^[trace-…
if the value is out of range; the +Range+ form will raise a
Hope that helps.
James Edward G. II
I would also recommend anchoring your regexp. In fact, you will probably
get
better performance by anchoring your RE and removing the splice (unless
the
lines are a LOT longer than your example line). If you anchor your
regexp to
the beginning of the line, the engine will stop trying to match as soon
as it
cannot match the [trace-. To do this, start the regexp with a ^ i.e.
/^[trace-…
Tim
The proper use of slice fixed it! I knew it was something retarded-ly
simple…
I really appreciate your help everyone. The energy behind this language
is amazing. Thanks for helping a stupid noob!
Also the anchoring note… Good call. I’m am actually parsing a
proprietary message framework. Some of the lines will be megabytes in
length…
Actually, that’s not correct. I didn’t realize there’s a
Regexp#match too that works similarly. Besides, your exception would
have been different had that been the case.
The simplest answer is that your regexp isn’t getting matched, so #match isn’t returning a MatchData object. You’ll have to figure out
why.
-Drew
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.