# N00b: text processing from magnetic stip input

**URL:** <https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321>\
**Category:** Ruby\
**Created:** [February 11, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321 "2010-02-11T19:12:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 11, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321/1 "2010-02-11T19:12:00Z")

</div>

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.

🙂

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 11, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321/2 "2010-02-11T19:26:06Z")

</div>

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. 🙂

> 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 11, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321/3 "2010-02-11T19:29:39Z")

</div>

On Thu, Feb 11, 2010 at 7:12 PM, John G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://ruby-doc.org/core/classes/String.html#M000778)

For example:

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

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 11, 2010, 8:07pm UTC](https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321/4 "2010-02-11T20:07:07Z")

</div>

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

> Posted via [http://www.ruby-forum.com/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 11, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/n00b-text-processing-from-magnetic-stip-input/183321/5 "2010-02-11T20:01:24Z")

</div>

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
