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:dirkh (d_he@hotmail.com)
Date:01/28/2008 12:29:40 PM
List:com.googlegroups.sketchupruby

Ok, that is 'programming to get the result'. I agree that sometimes it's faster to write a few lines of code that do exactly (and clearly) what is required. However, seeing the Regexp- power that Ruby has, I was convinced that Ruby must have implemented this. And it has!

Actually, when you 'match' a Regexp, the matching patterns results are stored into $1, $2, etc. *if* they correspond to grouped substrings.

An example:

TEST/d+ will match the strings composed of 'TEST' followed by any number of digits ( TEST1 or TEST999 for instance)

TEST(/d+) will do exactly he same, but because the digits are grouped between brackets, the resulting match is stored in $1. Thus, this Regexp matches TEST1 or TEST999 and $1 contains 1 or 999

If there are more than one bracketed groups, the results are in $1 $2 ...

For those interested: have a look at the Ruby scan() method.

On Jan 28, 12:46 am, Todd Burch - Katy, Texas wrote:

$& will contain the part of the string that matches.  If running the above, then $& will contain the whole string "TEST(3)".

Now that $& contains "TEST(3)", you'll have to use a second regex to get the number:

testpattern = Regexp.new('^TEST\(\d+\)$') elementundertest = 'TEST(345)' mymatch = testpattern.match(elementundertest) ;  # match? if  mymatch         puts "OK Match"         puts "matching substring = #{mymatch}"         puts "dollar & = #{$&}." ;         num = Regexp.new('\d+') ;         mynum = num.match($&) ;         puts "mynum = #{mynum}." ; else         puts "No match" end

Todd

On Jan 27, 3:12 pm, dirkh wrote:

Oops! Sorry, I made an error when copying my sample code... (The problem is *not* the 'TEST or 'TB' thing, that's a misunderstanding due to my error.)

This is the correct code:

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

So, I really want to match something that looks like "TEST(nn)", and I'm trying to get out the 'nn' value after the match has been detected...

(By the way, the Regexp is not overkill in the real application. This code sample is just a 'simplified version' of my problem.)

On Jan 27, 9:48 pm, Todd Burch - Katy, Texas wrote:

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

what do I need to use to obtain the '3'?- Hide quoted text -

- Show quoted text -- Hide quoted text -