Sorry nother regexp/ruby problem

K

Kev Jackson

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
'+');')

I'm thinking that a split will be more productive, but then I've got to
merge everything back together, sub + friends seems to be easier
(although I'm struggling to get exactly what I want as output)

Any thoughts + best practices/idioms etc would be very much appreciated

Kev

PS sorry to repeatedly spam on about this, but I have a deadline and I'd
rather be using Ruby than C, Java, Python, Perl
 
R

Robert Klemme

Kev said:
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?).

Kind regards

robert
 
K

Kev Jackson

I'm thinking that a split will be more productive, but then I've got
to merge everything back together, sub + friends seems to be easier
(although I'm struggling to get exactly what I want as output)
Hmm 1/2 hour messing with sub, or 2 minutes - split + join(", ") perfect

Sorry again
Kev
 
K

Kevin Brown

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
'+');')

I'm thinking that a split will be more productive, but then I've got to
merge everything back together, sub + friends seems to be easier
(although I'm struggling to get exactly what I want as output)

Any thoughts + best practices/idioms etc would be very much appreciated

I'd go with split

data = split(',', "'817-017', 800000, 0, 0, 200000, 200000, 500000, 500000,
500000, 500000, 60000, 59122, 0, 800000, '817'")

# process here

data.each{ |part|
string << part
}
 
K

Kev Jackson

Robert said:
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)

kev
 
G

Gene Tani

just FYI here're reg ex tools that i've found helpful (not
ruby-specific, but PCRE / perl-compatible. Ruby RE has a few deltas
from perl, which escape me at the moment besides lookbehind:

http://www.weitz.de/regex-coach/
http://samuelfullman.com/team/php/tools/regular_expression_tester_p2.php

http://aspn.activestate.com/ASPN/docs/Komodo/3.1/komodo-doc-regex.html


Kevin said:
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
'+');')

I'm thinking that a split will be more productive, but then I've got to
merge everything back together, sub + friends seems to be easier
(although I'm struggling to get exactly what I want as output)

Any thoughts + best practices/idioms etc would be very much appreciated

I'd go with split

data = split(',', "'817-017', 800000, 0, 0, 200000, 200000, 500000, 500000,
500000, 500000, 60000, 59122, 0, 800000, '817'")

# process here

data.each{ |part|
string << part
}
Kev

PS sorry to repeatedly spam on about this, but I have a deadline and I'd
rather be using Ruby than C, Java, Python, Perl
 
R

Robert Klemme

Kev said:
Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top