n00b: text processing from magnetic stip input

Discussion in 'Ruby' started by John Gesimondo, Feb 11, 2010.

  1. Hello everyone!

    I'm trying to make a program and I'm really bad at text processing. The
    input is a magnetic stripe of a card (coming straight off a card
    reader). I'm trying to split the output so that each part can get stored
    in a variable.

    Here's what comes off the card:
    %B6007503004905860^GEVIMSEDO/JRPN
    P^894212333300?;604750230190488515=993612044400050600?+E?

    And here's what I want (with relevant details):

    iso = 6007503004905860 [always the same length]

    last_name = Gevimsedo [variable length, always following carrot symbol]

    first_name = Jrpn [variable length, always after slash]

    middle_name = P [ either one letter, or non-existent after frist name
    separated by a space]


    If anyone could make that happen, you'd make my day/week/etc. I've been
    trying for a while, but I'm clearly too much of a n00b.

    :)
    --
    Posted via http://www.ruby-forum.com/.
     
    John Gesimondo, Feb 11, 2010
    #1
    1. Advertising

  2. John Gesimondo wrote:
    > Hello everyone!
    >
    > I'm trying to make a program and I'm really bad at text processing.


    Then you will have to get better. :)

    > The
    > input is a magnetic stripe of a card (coming straight off a card
    > reader). I'm trying to split the output so that each part can get stored
    > in a variable.
    >
    > Here's what comes off the card:
    > %B6007503004905860^GEVIMSEDO/JRPN
    > P^894212333300?;604750230190488515=993612044400050600?+E?
    >
    > And here's what I want (with relevant details):
    >
    > iso = 6007503004905860 [always the same length]
    >
    > last_name = Gevimsedo [variable length, always following carrot symbol]
    >
    > first_name = Jrpn [variable length, always after slash]
    >
    > middle_name = P [ either one letter, or non-existent after frist name
    > separated by a space]
    >
    >
    > If anyone could make that happen, you'd make my day/week/etc.


    Do you mean that you expect us to write your core processing code for
    you? If so, that's not cool. We can help, but you'll have to do the
    work yourself.

    > I've been
    > trying for a while, but I'm clearly too much of a n00b.
    >
    > :)


    Step 1: Write specs with RSpec (or similar library) defining the desired
    behavior.
    Step 2: Read the documentation for classes String and Regexp.
    Step 3: Make the tests pass.

    If you have problems, share what you've done and we'll be happy to help.
    Good luck!

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Feb 11, 2010
    #2
    1. Advertising

  3. On Thu, Feb 11, 2010 at 7:12 PM, John Gesimondo <> wrote:
    > Hello everyone!
    >
    > I'm trying to make a program and I'm really bad at text processing. The
    > input is a magnetic stripe of a card (coming straight off a card
    > reader). I'm trying to split the output so that each part can get stored
    > in a variable.
    >
    > Here's what comes off the card:
    > %B6007503004905860^GEVIMSEDO/JRPN
    > P^894212333300?;604750230190488515=993612044400050600?+E?
    >
    > And here's what I want (with relevant details):
    >
    > iso = 6007503004905860 [always the same length]
    >
    > last_name = Gevimsedo [variable length, always following carrot symbol]
    >
    > first_name = Jrpn [variable length, always after slash]
    >
    > middle_name = P [ either one letter, or non-existent after frist name
    > separated by a space]
    >
    >
    > If anyone could make that happen, you'd make my day/week/etc. I've been
    > trying for a while, but I'm clearly too much of a n00b.


    Show us what you tried, it's easier to help that way. Anyway, you
    could build a regular expression that matches the fixed parts of the
    string while capturing in groups your "variables". To get you started:

    regex = %r{\%B(.*)\^(.*)/ [...and so on...] }

    then match the string against that regex using:

    http://ruby-doc.org/core/classes/String.html#M000778

    For example:

    match = yourstring.match(regex)
    puts match.captures

    Hope this helps,

    Jesus.
     
    Jesús Gabriel y Galán, Feb 11, 2010
    #3
  4. Sorry about the confusion. Apparently I asked the question wrong. I was
    just looking for some help with the regular expression part. I didn't
    know how to extract variables from a long string.

    > regex = %r{\%B(.*)\^(.*)/ [...and so on...] }


    This was quite helpful. I was able to figure this out:

    %r{\%B(\d*)\^(\S*)\/(\S*)\s([a-zA-Z])}

    So I think the problem is solved! Because I *think* I know how to
    integrate this into my program.


    Thanks for the help, and I'll be sure to include my attempts next time.
    And please let me know if you noticed anything wrong with that regex.

    John
    --
    Posted via http://www.ruby-forum.com/.
     
    John Gesimondo, Feb 11, 2010
    #4
  5. On Feb 11, 2010, at 2:01 PM, John Gesimondo wrote:

    > Sorry about the confusion. Apparently I asked the question wrong. I
    > was
    > just looking for some help with the regular expression part. I didn't
    > know how to extract variables from a long string.
    >
    >> regex = %r{\%B(.*)\^(.*)/ [...and so on...] }

    >
    > This was quite helpful. I was able to figure this out:
    >
    > %r{\%B(\d*)\^(\S*)\/(\S*)\s([a-zA-Z])}
    >
    > So I think the problem is solved! Because I *think* I know how to
    > integrate this into my program.
    >
    >
    > Thanks for the help, and I'll be sure to include my attempts next
    > time.
    > And please let me know if you noticed anything wrong with that regex.
    >
    > John
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    Well, break it into parts... I'll use %r{ } for Regexp literals

    You also haven't captured all the information. Try this:

    starts with '%B' : %r{\A%B}
    >> iso = 6007503004905860 [always the same length]

    16 digits
    iso = %r{(\d{16})}
    >> last_name = Gevimsedo [variable length, always following carrot
    >> symbol]

    last_name = %r{\^([^/]*)}
    >> first_name = Jrpn [variable length, always after slash]

    first_name = %r{/([^ ^]*)}
    >> middle_name = P [ either one letter, or non-existent after frist name
    >> separated by a space]

    middle_name = %r{(?: ([^ ^]))?}
    Then a caret.

    Put that all together:

    irb> re = %r{\A%B(\d{16})\^([^/]*)/([^ ^]*)(?: ([^ ^]))?\^}
    => /\A%B(\d{16})\^([^\/]*)\/([^ ^]*)(?: ([^ ^]))?\^/
    irb> card_data = '%B6007503004905860^GEVIMSEDO/JRPN P^894212333300?;
    604750230190488515=993612044400050600?+E?'
    => "%B6007503004905860^GEVIMSEDO/JRPN P^894212333300?;
    604750230190488515=993612044400050600?+E?"
    irb> iso, last_name, first_name, middle_name =
    re.match(card_data).captures
    => ["6007503004905860", "GEVIMSEDO", "JRPN", "P"]
    irb> p iso, last_name, first_name, middle_name
    "6007503004905860"
    "GEVIMSEDO"
    "JRPN"
    "P"
    => nil

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Feb 11, 2010
    #5
    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. Brian
    Replies:
    2
    Views:
    532
    Brian
    May 3, 2004
  2. HappyHippy
    Replies:
    6
    Views:
    766
    Bruno Desthuilliers
    Feb 13, 2006
  3. Replies:
    2
    Views:
    640
    Rodrigo Zechin
    Oct 17, 2006
  4. moostafa

    n00b input help

    moostafa, Apr 1, 2006, in forum: C Programming
    Replies:
    5
    Views:
    299
    Ben Bacarisse
    Apr 2, 2006
  5. RedEye

    Magnetic Strip Reader API for CF

    RedEye, Sep 7, 2005, in forum: ASP .Net Mobile
    Replies:
    5
    Views:
    210
    Dick Grier
    Sep 7, 2005
Loading...

Share This Page