# Printing part what's found with a regex

**URL:** <https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143>\
**Category:** Ruby\
**Created:** [September 17, 2010, 4:29pm UTC](https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143 "2010-09-17T16:29:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 17, 2010, 4:29pm UTC](https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143/1 "2010-09-17T16:29:20Z")

</div>

Hi everyone,

I’m trying to build a program that filters  
certain lines through regular expressions.  
In the seconde regular expression I wan to  
be able to print only what’s within the  
(\w+) part of the expression?

How do I do this?

counter = 0  
if inFile.nil? == false  
File.foreach(inFile) do |line|  
if line =~ /^[^%\s\*^%]/  
if line =~ /(?!%)\w+\s+(\w+)/  
puts "found in line #{counter+1} "  
puts line  
end  
end  
counter = counter + 1  
end  
end

Regards,  
Ted.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 17, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143/2 "2010-09-17T16:38:03Z")

</div>

You will find that portions of the regex marked with parentheses will be  
places in variables called $1, $2, etc. So, you will want to change the  
regex to

/(?!%)\w+\s+((\w+))/

And then the contents of ((\w+)) will be placed in the variable $2.

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 17, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143/3 "2010-09-17T16:58:11Z")

</div>

Jonathan N. wrote:

> You will find that portions of the regex marked with parentheses will be  
> places in variables called $1, $2, etc. So, you will want to change the  
> regex to
> 
> /(?!%)\w+\s+((\w+))/
> 
> And then the contents of ((\w+)) will be placed in the variable $2.
> 
> -Jonathan N.

hmm I’m trying to print this variable  
but I get nothing. I’ve tried:

puts “#$2”  
puts “#{$2}”

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 17, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143/4 "2010-09-17T17:01:04Z")

</div>

John S. wrote:

> Jonathan N. wrote:
> 
> > You will find that portions of the regex marked with parentheses will be  
> > places in variables called $1, $2, etc. So, you will want to change the  
> > regex to
> > 
> > /(?!%)\w+\s+((\w+))/
> > 
> > And then the contents of ((\w+)) will be placed in the variable $2.
> > 
> > -Jonathan N.
> 
> hmm I’m trying to print this variable  
> but I get nothing. I’ve tried:
> 
> puts “#$2”  
> puts “#{$2}”

Ah just realized it is in variable $1.  
(?!%) is a look ahead so I guess it doesn’t count as a group?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 17, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/printing-part-whats-found-with-a-regex/195143/5 "2010-09-17T17:19:02Z")

</div>

On Fri, Sep 17, 2010 at 9:04 AM, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ah just realized it is in variable $1.  
> (?!%) is a look ahead so I guess it doesn’t count as a group?

Ah yes, that’ll be it. Sorry, I wasn’t paying attention to what was  
inside  
that first group. Glad you got it figured out.

-Jonathan N.
