# Regex problem... Tried Searching forum already... :(

**URL:** <https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573>\
**Category:** Ruby\
**Created:** [March 29, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573 "2007-03-29T23:45:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 29, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/1 "2007-03-29T23:45:59Z")

</div>

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.

thanks for looking

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 29, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/2 "2007-03-29T23:55:18Z")

</div>

Jon wrote:

> ```
> puts reg.match(line)[1]
> 
> ```

Try this:

puts line.match(reg)[1]

The #match method is in String, not Regexp.

-Drew

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 29, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/3 "2007-03-29T23:58:18Z")

</div>

Jon wrote:

> I get this:  
> undefined method `[]’ for nil:NilClass (NoMethodError)  
> on the reg.match[1] line.

Is is possible that you have a string that doesn’t match? In that case  
match will return a nil object.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 30, 2007, 12:05am UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/4 "2007-03-30T00:05:19Z")

</div>

On Mar 29, 2007, at 4:46 PM, Jon wrote:

> 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.

No need to feel bad. We will figure it out…

> Here is an example string:
> 
> [trace-2932] Application=3071000

First, let’s be sure the expression matches:

> > “[trace-2932] Application=3071000” =~ /[trace-(\d+)]/  
> > =\> 0

Yep. That’s not our problem.

So let’s look into what slice!() is doing to your String:

> > str = “[trace-2932] Application=3071000”  
> > =\> “[trace-2932] Application=3071000”
> 
> > str.slice!(0, 25)  
> > =\> “[trace-2932] Application=”
> 
> > str  
> > =\> “3071000”

Bingo. That’s the problem. See it now?

Here is the documentation for slice!():

## $ 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"

```

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 30, 2007, 6:50am UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/5 "2007-03-30T06:50:52Z")

</div>

James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > def go(lines)
> 
> ```
> 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

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 30, 2007, 8:32am UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/6 "2007-03-30T08:32:30Z")

</div>

Tim X wrote:

> James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > > def go(lines)
> > 
> > ```
> > 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…

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [March 30, 2007, 12:08am UTC](https://www.ruby-forum.com/t/regex-problem-tried-searching-forum-already/90573/7 "2007-03-30T00:08:23Z")

</div>

Drew R. wrote:

> Jon wrote:
> 
> > ```
> > puts reg.match(line)[1]
> > 
> > ```
> 
> Try this:
> 
> puts line.match(reg)[1]
> 
> The #match method is in String, not Regexp.

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
