提取字符串中的数字
需要从以下字符串中把里面的数字提取出来,字符串为:d:/test/aero-m0.2a0b2.5h3mass0.5提取出来的结果放到数组中:
[0.2 0 2.5 3 0.5];
尝试了使用sscanf未果,还请高手帮忙指点,谢谢
#include <stdio.h> void foo( const char* s ) { printf( "\"%s\":\n\t", s ); for( int pos=0; ; ) { int newpos = 0; int n = sscanf( s+pos, "%*[^0-9]%n", &newpos ); if( n == EOF ) break; pos += newpos; double val; n = sscanf( s+pos, "%lf%n", &val, &newpos ); if( n == EOF ) break; if( n != 0 ) pos += newpos; printf( " %g", val ); } putchar( '\n' ); } int main( void ) { const char* s = "d:/test/aero-m0.2a0b2.5h3mass0.5"; foo( s ); foo( "" ); foo( "abc" ); foo( "123" ); foo( "abc123" ); foo( "123abc" ); foo( "abc123def456" ); foo( "abc123def456ghi" ); foo( "123abc456def" ); foo( "123abc456def789" ); }