From: Robert Klemme Date: 2005-10-04T22:36:47+09:00 Subject: Re: Sorry nother regexp/ruby problem Kev Jackson wrote: > Robert Klemme wrote: > >> Kev Jackson wrote: >> >> >>> Given the following data >>> >>> '817-017', 800000, 0, 0, 200000, 200000, 500000, 500000, 500000, >>> 500000, 60000, 59122, 0, 800000, '817' >>> >>> How can I easily just select the first 4 elements (where elements >>> 2,3 and 4 could be NULL)? >>> >>> At the moment I'm trying a regexp of the form >>> >>> line.sub!(/(VALUES \(\s*'\w',\s*[0-9]+,\s*[0-9]+,\s*[0-9]+|NULL).+/, >>> '\1 '+');') >>> >>> >> >> Why do you use VALUES in the regexp? I cannot see it in your sample >> data. It would be easier if you post sample output that you want to >> see. I'm not sure about the "first 4" elements (is it characters? >> is it items in the list?). >> >> > Sorry about that, the original string is part of an insert statement > INSERT INTO TABLE (FIELD, FIELD, FIELD) VALUES (STUFF, STUFF, STUFF > etc...); > > -------------------------------------- > > regexp to munge this bit > > the first 4 elements would be '817-017', 800000, 0, 0 > > As someone else pointed out, split is a better tool for this job (and > indeed having resolved the problem with split and join I now see my > folly trying to use a regexp) If it's guaranteed that there are no "," in STUFF you might cook up a regexp but split is certainly simpler. robert