正则表达式:匹配c中的浮点数

时间:2014-11-10 12:36:09

标签: c regex

我使用以下两个正则表达式来匹配数字(例如,1.2,1 ...)

regex_text0 = "[0-9]+|\.[0-9]+|[0-9]+\.[0-9]";

regex_text = "[0-9]+|[.][0-9]+|[0-9]+[.][0-9]+";

以下函数执行它。

static int match_regex (regex_t * r, const char * to_match)
{
const char * p = to_match;
const int n_matches = 10;
regmatch_t m[n_matches];

while (1) {
    int i = 0;
    int nomatch = regexec (r, p, n_matches, m, 0);
    if (nomatch) {
        printf ("No more matches.\n");
        return nomatch;
    }

    for (i = 0; i < n_matches; i++) {
        int start;
        int finish;
        if (m[i].rm_so == -1) {
            printf("break ");
            break;
        }
        start = m[i].rm_so + (p - to_match);
        finish = m[i].rm_eo + (p - to_match);
        if (i == 0) {
            printf ("$& is ");
        }
        else {
            printf ("$%d is ", i);
        }
        printf ("'%.*s' (bytes %d:%d)\n", (finish - start),
            to_match + start, start, finish);
    }
    p += m[0].rm_eo;
}
return 0;
}

但是两个正则表达式的结果是不同的。

Trying to find '[0-9]+|[.][0-9]+|[0-9]+[.][0-9]+' in '1.0 + 2.3'                                                                                
$& is '1.0' (bytes 0:3)                                                                                                                         
break                                                                                                                                           
$& is '2.3' (bytes 6:9)                                                                                                                         
break                                                                                                                                           
No more matches.                                                                                                                                
Trying to find '[0-9]+|.[0-9]+|[0-9]+.[0-9]' in '1.0 + 2.3'                                                                                     
$& is '1.0' (bytes 0:3)                                                                                                                         
break                                                                                                                                           
$& is ' 2' (bytes 5:7)                                                                                                                          
break                                                                                                                                           
$& is '.3' (bytes 7:9)                                                                                                                          
break                                                                                                                                           
No more matches.  

问题:

  • 这两个正则表达式的区别是什么?
  • 为什么regexec()每次匹配一个号码,但regexec()的原型需要一个数组的regmatch_t

1 个答案:

答案 0 :(得分:0)

您的第二个正则表达式[0-9]+|[.][0-9]+|[0-9]+[.][0-9]+允许12.2113.131这样的数字,其中第一个只接受13.11.1,即{{1}后的一位数如果在.之前存在一个数字。这是因为第二个正则表达式末尾的.符号。这是两个正则表达式之间唯一的区别。

相关问题