glib库的字符串替换函数

引用自:

http://www.diybl.com/course/6_system/linux/Linuxjs/20090317/162066.html

 

首先建立要字符串替换的文件 test_regex.txt
11aa222bb33333cccc44444dddddddd

 

在建立测试类:

 

/**
*gcc -g -o g_regex g_regex.c `pkg-config --libs --cflags gtk+-2.0`
**/
#include <glib.h>

static void print_uppercase_words(const gchar* string)
{
        GRegex* regex;
        GMatchInfo *match_info;
        GError *error = NULL;
        regex = g_regex_new("[0-9]+", 0 , 0, NULL);
        g_regex_match(regex, string, 0, &match_info);
        while (g_match_info_matches(match_info)) {
                gchar* word = g_match_info_fetch(match_info, 0);
                g_print("%s\n",word);
                g_free(word);
                g_match_info_next(match_info, NULL);
        }
        g_match_info_free(match_info);
        g_regex_unref(regex);
}
static void replace_test(const gchar* string)
{
    gchar**  match;
      gchar*  url;
    match = g_regex_split_simple("s",string,0,0);
    for (url = NULL; (url = *match); match++)
      {
        g_print ("url is %s\n",url);
      }
}
static void replace_test_full(const gchar* string)
{
    gchar**  match;
    gchar*  content;
    static GRegex *content_regex = NULL;
    content_regex = g_regex_new("<externalId>.*</externalId>",
                                G_REGEX_OPTIMIZE, 0, NULL);


    content = g_regex_replace(content_regex, string,
                        strlen(string), 0, "<externalId/>", 0, NULL);

    g_print ("content is %s\n",content);
}
int main()
{
        char *buf;
        int length;
        g_file_get_contents("test_regex.txt", &buf, &length,NULL);
        print_uppercase_words(buf);
        // 简单替换
        replace_test("123s456s789ss");
        // 使用正则表达式替换
        replace_test_full("<user><userid>27095572</userid><imageurl>http://c4.ac-images.myspacecdn.com/images02/123/s_54515716e7394681a3d7510807c8608b.jpg</imageurl><profileurl>http://www.myspace.com/michaelkhanmusic</profileurl><name>Michael Khan</name><mood>(none)</mood><moodimageurl/><moodlastupdated>2010/1/10 17:48:04</moodlastupdated><status>crap, back to school. i&amp;#39;m gonna have to wake up the time i usually go to sleep..</status><statusId>633987424840000000</statusId><updateTicks>316460884</updateTicks><externalId>?O??&#x1;&#x0;&#x0;&#x0;</externalId><appId/><sourceName>Twitter</sourceName><sourceUrl>http://twitter.com/michaelkhan</sourceUrl><sourceImageUrl>http://x.myspacecdn.com/modules/activityingestion/static/img/sync/twitter.png</sourceImageUrl></user>");
        return 0;
}
 

程序使用起来非常简单,3步就可以搞定

1.创建一个GRegex,来定义你的正则表达式,这里定义了只匹配所有数字。

2.使用 g_regex_match来匹配内容中符合正则表达式规则的所有内容。

3.因为匹配出来的是一个集合,利用 g_match_info_fetch把每一项fetch出来

 

编译:

gcc -g -o g_regex g_regex.c `pkg-config --libs --cflags gtk+-2.0`

运行结果:

[root@localhost test-core]$ ./g_regex
11
222
33333
44444
url is 123
url is 456
url is 789
url is
url is

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值