繁体   English   中英

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

[英]Regex: match a float number in c

我正在使用以下两个正则表达式来匹配数字 (例如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()的原型需要一个regexec() 数组

您的第二个正则表达式[0-9]+|[.][0-9]+|[0-9]+[.][0-9]+允许使用数字12.2113.131 ,其中第一个仅接受13.11.1即后面一位. 如果之前有一个数字. 这是因为第二个正则表达式末尾有+号,这是两个正则表达式之间的唯一区别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM