11 messages in com.googlegroups.sketchuprubyRe: 'match' and predefined variables
FromSent OnAttachments
dirkh27 Jan 2008 12:09 
Todd Burch - Katy, Texas27 Jan 2008 12:48 
dirkh27 Jan 2008 13:11 
Todd Burch - Katy, Texas27 Jan 2008 15:46 
dirkh28 Jan 2008 12:29 
dirkh28 Jan 2008 12:33 
Todd Burch - Katy, Texas28 Jan 2008 13:00 
dirkh28 Jan 2008 14:07 
Todd Burch - Katy, Texas28 Jan 2008 15:04 
dirkh29 Jan 2008 10:39 
Todd Burch - Katy, Texas29 Jan 2008 12:19 
Subject:Re: 'match' and predefined variables
From:Todd Burch - Katy, Texas (mr.t@gmail.com)
Date:01/27/2008 12:48:11 PM
List:com.googlegroups.sketchupruby

For one, you need a looser regex to make it work with TB(3). It will always fails with TEST in your regex.

In your current regex, you are looking for 'TEST(', followed by 1 or more numbers, followed by ')', when comparing to 'TEST(3)', the whole string will match, and that is what resides in $&.

Changing the regex to Regexp.new('\(\d+\)'), and testing the same "TEST(3)", then "(3)" will reside in $&.

regex seems to be overkill for this simple parsing job.

Todd

On Jan 27, 2:10 pm, dirkh wrote:

I know that it is possile, but I can't figure out how to do it:

I test a string against a pattern, and when a match is found, I would like to find the 'matching value' of the string... An example will probably clarify:

I have a Regexp testpattern that represents "TEST(n)" where n can be any string of digits. Thus, "TEST(1)" or "TEST(45)" would match. What I would like to obtain is the 'value' that made it match. Thus '1' or '45'.

Clearly, the $~ predefined works to get me the whole string. I thought that the $1 would give me the substring, but it seems not...  What do I miss out?

This is my test-code:

testpattern = Regexp.new ('^TEST\(\d+\)$') elementundertest = 'TB(3)' if testpattern.match(elementundertest) then  puts "OK Match"  puts "elementundertest= #$~"  ##### works fine, yields TB(3) #####  puts "matching substring = #$1"  ##### doesn't work ##### else  puts "No match" end