windguy 发表于 2007-11-30 10:53:36

正则表达式的模式

我有一个字符串:"test"   "todo"
使用pattern      :^\".*?\"
希望找到字符串:"test"

可是找到的是整个字符串:"test"   "todo"
使用linux下面的regcomp 和regexec ,
请问 怎么构造pattern才能找到需要的字符串?

hiwoody 发表于 2008-2-19 09:35:45

你所给的两个字符串都是匹配的
这个应该可以了
:^\".*?t\"

涩兔子 发表于 2008-2-25 15:49:54

你需要使用偏移量,man regexec里
BYTE OFFSETS el
       UnlessREG_NOSUBwas set for the compilation of the pattern buffer, it is possible to obtain substring match addressing information.
       pmatch must be dimensioned to have at least nmatch elements.These are filled in by regexec() with substringmatchaddresses.   Any
       unused structure elements will contain the value -1.

       The regmatch_t structure which is the type of pmatch is defined in regex.h.

            typedef struct
            {
                regoff_t rm_so;
                regoff_t rm_eo;
            } regmatch_t;

       Eachrm_soelementthatisnot -1 indicates the start offset of the next largest substring match within the string.The relative
       rm_eo element indicates the end offset of the match.

请查看
http://trac.lcuc.org.cn/public/trac-sirch/browser/trunk/util/test/test_filesystem.c
中reg_replace如何替换HTML的,reg_replace工具在http://trac.lcuc.org.cn/public/trac-sirch/browser/trunk/util/regular.c
页: [1]
查看完整版本: 正则表达式的模式