From: "Jesús Gabriel y Galán" Date: 2010-02-12T03:28:08+09:00 Subject: Re: n00b: text processing from magnetic stip input 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.