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.
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.
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}”
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?
On Fri, Sep 17, 2010 at 9:04 AM, John S. [email protected] 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.