开发者

Use regular expressions with Glib

开发者 https://www.devze.com 2022-12-24 05:09 出处:网络
I would like to find all comment blocks(/*...*/) but the function g_regex_match_full always returns true.

I would like to find all comment blocks(/*...*/) but the function g_regex_match_full always returns true. Here is the code :

// Create the regex.
start_block_comment_regex = g_regex_new("/\*.*\*/", G_REGEX_OPTIMIZE, 0, &regex_error);

//Search the regex;
if(TRUE == g_regex_match_ful开发者_开发知识库l(start_block_comment_regex, current_line, -1, 0, 0, &match_info, &regex_error))
{
}


You're not using the pattern you think you are. You have to escape backslashes in strings in C:

comment_regex = g_regex_new("/\\*.*\\*/", G_REGEX_OPTIMIZE, 0, &regex_error);

I'm surprised you don't get compiler warnings about "undefined escape sequence \*" from your current code. I'm also surprised you didn't get errors from glib there - the pattern you effectively used was probably /*.**/, which doesn't make much sense. (Did you check regex_error? Obviously didn't if that's the full code...)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号