Sorry nother regexp/ruby problem

Discussion in 'Ruby' started by Kev Jackson, Oct 4, 2005.

  1. Kev Jackson

    Kev Jackson Guest

    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
     
    Kev Jackson, Oct 4, 2005
    #1
    1. Advertising

  2. 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?).

    Kind regards

    robert
     
    Robert Klemme, Oct 4, 2005
    #2
    1. Advertising

  3. Kev Jackson

    Kev Jackson Guest


    >
    > 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
     
    Kev Jackson, Oct 4, 2005
    #3
  4. Kev Jackson

    Kevin Brown Guest

    On Tuesday 04 October 2005 02:29, 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
    > '+');')
    >
    > 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
     
    Kevin Brown, Oct 4, 2005
    #4
  5. Kev Jackson

    Kev Jackson Guest

    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)

    kev
     
    Kev Jackson, Oct 4, 2005
    #5
  6. Kev Jackson

    Gene Tani Guest

    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 Brown wrote:
    > On Tuesday 04 October 2005 02:29, 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
    > > '+');')
    > >
    > > 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
     
    Gene Tani, Oct 4, 2005
    #6
  7. 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
     
    Robert Klemme, Oct 4, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob in PA

    'nother css question

    Bob in PA, Jan 11, 2004, in forum: HTML
    Replies:
    4
    Views:
    401
    William Tasso
    Jan 11, 2004
  2. Richard Lamboj

    starting a thread in a nother thread

    Richard Lamboj, Jan 27, 2010, in forum: Python
    Replies:
    7
    Views:
    288
  3. Greg Brondo

    'nother question on erb/eruby

    Greg Brondo, Apr 8, 2004, in forum: Ruby
    Replies:
    4
    Views:
    128
    Mark Hubbart
    Apr 9, 2004
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    541
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    402
    7stud --
    Aug 21, 2009
Loading...

Share This Page