将正则表达式与 Glib 结合使用
我想找到所有注释块(/*...*/),但函数 g_regex_match_full 始终返回 true。 这是代码:
// Create the regex.
start_block_comment_regex = g_regex_new("/\*.*\*/", G_REGEX_OPTIMIZE, 0, ®ex_error);
//Search the regex;
if(TRUE == g_regex_match_full(start_block_comment_regex, current_line, -1, 0, 0, &match_info, ®ex_error))
{
}
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, ®ex_error);
//Search the regex;
if(TRUE == g_regex_match_full(start_block_comment_regex, current_line, -1, 0, 0, &match_info, ®ex_error))
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你没有使用你认为的模式。您必须转义 C 中字符串中的反斜杠:
我很惊讶您没有从当前代码中收到有关“未定义转义序列
\*
”的编译器警告。我也很惊讶你没有从 glib 那里得到错误 - 你有效使用的模式可能是/*.**/
,这没有多大意义。 (你检查过 regex_error 吗?如果这是完整的代码,显然没有检查......)You're not using the pattern you think you are. You have to escape backslashes in strings in C:
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...)