Printing part what's found with a regex

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.

···

--
Posted via http://www.ruby-forum.com/.

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 Nielsen

Jonathan Nielsen 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 Nielsen

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

puts "#$2"
puts "#{$2}"

···

--
Posted via http://www.ruby-forum.com/\.

John Smith wrote:

Jonathan Nielsen 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 Nielsen

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?

···

--
Posted via http://www.ruby-forum.com/\.

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 Nielsen

···

On Fri, Sep 17, 2010 at 9:04 AM, John Smith <edvelez.g@gmail.com> wrote:

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