|
|
Re: one-liner to parse an alpha-numeric string
Posted:
Nov 21, 2005 3:51 PM
|
|
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
|
|