Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: one-liner to parse an alpha-numeric string
Replies: 5   Last Post: Nov 22, 2005 12:41 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Duane Bozarth

Posts: 298
Registered: 1/25/05
Re: one-liner to parse an alpha-numeric string
Posted: Nov 21, 2005 3:51 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Jeff Kiecker wrote:
>
> Hey folks, got a question. I've spent several hours trying to figure
> this out (i.e., trying to understand format strings and such) with no
> success. Let me know if this can be done.
>
> I'd like to strip out several numbers from a string to use in future
> calculations. The caveat is I'd like to do this with a single line
> of code for each number (no if/then's, etc.)
>
> the string is basically denotes a tire size: 'P225/70R17',
> 'LT265/50R18', 'P245/45ZR18', etc.
>
> I would like to strip out each number and str2num it. The above
> three examples cover all the string possibilities prettymuch in that
> there is always one or two letters to start, and the 'R' sometimes
> has a letter before it.
>
> BUT, the first number is always 3 digits, and the last two numbers
> are always 2 digits.
>
> I'm looking for a solution like this
> SectionWidth = ... = 225
> AspectRatio = ... = 70
> RimDiameters = ... = 17
>
> I think i can do this with sscanf, but i can't figure it out, can
> anyone help?



As noted, seems like to be very robust you would need to do something
that was based more closely on the string actually being parsed. After
my first thought, my second was based on another poster's question a
week or so ago and uses strtok in a loop.

What about something like the following as a basis for a function...the
delimiter string would need to include all possible nonnumeric values of
course.

» s='LT265/50R18'
s =
LT265/50R18
» [tok,s] = strtok(s,'TLR/')
tok =
265
s =
/50R18
» [tok,s] = strtok(s,'TLR/')
tok =
50
s =
R18
» [tok,s] = strtok(s,'TLR/')
tok =
18



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.