字符串处理

软件和网站开发以及相关技术探讨
回复
chuzhufei
帖子: 35
注册时间: 2009-04-01 8:50

字符串处理

#1

帖子 chuzhufei » 2009-06-26 10:21

文件格式如G10=(G1,G2,G3)
或者G10=(G1,G2)
后面括号中的个数不一定
现在需要分离出其中的数字该怎么办? 提取如 10 1 2 3 出来
头像
peachcolor
帖子: 898
注册时间: 2006-05-20 14:03

Re: 字符串处理

#2

帖子 peachcolor » 2009-06-26 10:46

比如文件text内容如下

代码: 全选

G10=(G1,G2,G3)
G10=(G1,G2)
终端这么搞一下

代码: 全选

cat text | sed 's/[=G]//g' | sed 's/[,=\(\)]/ /g'
输出就这样

代码: 全选

10 1 2 3 
10 1 2 
chuzhufei
帖子: 35
注册时间: 2009-04-01 8:50

Re: 字符串处理

#3

帖子 chuzhufei » 2009-06-26 14:00

peachcolor 写了:比如文件text内容如下

代码: 全选

G10=(G1,G2,G3)
G10=(G1,G2)
终端这么搞一下

代码: 全选

cat text | sed 's/[=G]//g' | sed 's/[,=\(\)]/ /g'
输出就这样

代码: 全选

10 1 2 3 
10 1 2 
C语言怎么实现啊
头像
Final_x
帖子: 383
注册时间: 2008-05-03 23:05
联系:

Re: 字符串处理

#4

帖子 Final_x » 2009-07-23 3:09

大概有个思路:逐字符扫描,
----若为数字:入栈
----若非数字:全部出栈
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 字符串处理

#5

帖子 eexpress » 2009-07-23 8:36

c也支持正则的啊。
搜索数字就是\d*
● 鸣学
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 字符串处理

#6

帖子 tusooa » 2009-07-25 6:26

代码: 全选

#include <stdio.h>
int main()
{
    char *l;
    scanf("%s", &l);
    for(int i=0;a[i] != "\0"; i++)
    {
        if (a[i]==0 || a[i]==1 || a[i]==2 || a[i] == 3 || a[i] == 4 || a[i]==5 || a[i] == 6 || a[i]==7|| a[i]== 8 || a[i]==9)
        {
            printf("%d  ", a[i]);
        }
    }
    printf("\n");
}

代码: 全选

] ls -ld //
头像
xiaocheng_zh
帖子: 46
注册时间: 2009-05-30 15:52
来自: DL LIAONING CHN

Re: 字符串处理

#7

帖子 xiaocheng_zh » 2009-07-25 15:28

ls的语言可以再精炼下下!

#include <ctype.c>
int main(int argc, char * argv[]){
/*bla bla……*/
if ( isdigit ( a ) ){
/*bla bla……*/

return 0;
}
回复