Buddy_Zhang1 发表于 2016-09-20 21:59

一个正则表达式,到底有什么不同

最近在研究 Kbuild 的词法分析器,遇到两个很相似的正则表达式,不知道他们的不同点在哪里,如下:
[^'"\\\n]+/\n和
[^'"\\\n]+对于上面两个正则表达式,我使用两个测试用例,第一个如下:
%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%%
[^'"\\\n]+/\n   { printf("Matchoo[%s]\n", yytext); }
[^'"\\\n]+      { printf("Matchi2[%s]\n", yytext); }
%%
int main(int argc, char *argv[])
{
    yylex();
    return 0;
}第二个测试用例为
%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%%
[^'"\\\n]+/\n   { printf("Matchoo[%s]\n", yytext); }
[^'"\\\n]+      { printf("Matchi2[%s]\n", yytext); }
%%
int main(int argc, char *argv[])
{
    /* Parsing a string. */
    char input = "Linux/$ARCH $KERNELVERSION Kernel Configuration";

    /* Copy string into new buffer and switch buffers. */
    yy_scan_string(input);

    /* Analyze the string. */
    yylex();

    /* Delete the new buffer. */
    yy_delete_buffer(YY_CURRENT_BUFFER);

    return 0;
}第一个测试用例处理一个来自终端输入的字符串:"Linux/$ARCH $KERNELVERSION Kernel Configuration"
第二个测试用例处理一个来自 buffer 的字符串:"Linux/$ARCH $KERNELVERSION Kernel Configuration"
同样的字符串,但是结果不同
第一个测试用例匹配了: [^'"\\\n]+/\n
第二个测试用例匹配了: [^'"\\\n]+

这两个测试用例有什么不同?



Buddy_Zhang1 发表于 2016-09-22 19:00

这个问题已经解决,解决过程在我的 github 上: https://github.com/BiscuitOS/Biscuit_Compiler/issues/16

nswcfd 发表于 2016-09-26 11:01

有点晕,貌似是\n的区别?来自终端的有\n结尾?
页: [1]
查看完整版本: 一个正则表达式,到底有什么不同