N00b: text processing from magnetic stip input

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.

:slight_smile:

John G. 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. :slight_smile:

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.

:slight_smile:

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
[email protected]

On Thu, Feb 11, 2010 at 7:12 PM, John G. [email protected] wrote:

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.

On Feb 11, 2010, at 2:01 PM, John G. wrote:

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 B. http://agileconsultingllc.com
[email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs