extract numbers from string
andrea977
I'm a newby in IGOR PRO.
I'm trying to extract a number from a string for data analysis pourpose.
I use this kind of instruction:
string COL=stringfromlist (0, lista, ";")
string field, value, tesla, degree, kelvin
String expr="([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)"
Splitstring/E=(expr) COL, field, value, tesla, degree, kelvin
variable/D B=str2num(value)
string field, value, tesla, degree, kelvin
String expr="([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)"
Splitstring/E=(expr) COL, field, value, tesla, degree, kelvin
variable/D B=str2num(value)
I have the string COL which is always in the form Magnetic4T77K
So the function returns field=magnetic, value=4, tesla=T, degree=77 and kelvin=K
I need the value of the value string so in this case the function works properly!
But if I have a string where the value is decimal as COL=Magnetic1.2T77K
the function does not work! It is clearly the dot creating problems!!
Can I use an alternative method to extract the value from the string or can I modify this function in some way?
Thank you
([[:alpha:]]+)([-.+[:digit:]]+)([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)
That expression allows the period, plus sign, and minus sign as well as digits in the second capturing group.
If you know you won't ever have the plus or minus sign, you could use:
([[:alpha:]]+)([.[:digit:]]+)([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)
If you expect decimals to be in other capturing groups, you'll need to do the same thing for those groups.
April 28, 2011 at 08:02 am - Permalink
sscanf
would make a much clearer solution:// fieldStrength, temperature, are variables that you declare
// and that will be set to the correct values by sscanf
sscanf arg, "Magnetic%gT%gK", fieldStrength, temperature
April 28, 2011 at 09:08 am - Permalink
thank you again
May 5, 2011 at 03:12 am - Permalink