Chinaunix

标题: 一个正则表达式,到底有什么不同 [打印本页]

作者: Buddy_Zhang1    时间: 2016-09-20 21:59
标题: 一个正则表达式,到底有什么不同
最近在研究 Kbuild 的词法分析器,遇到两个很相似的正则表达式,不知道他们的不同点在哪里,如下:
  1. [^'"\\\n]+/\n
复制代码

  1. [^'"\\\n]+
复制代码
对于上面两个正则表达式,我使用两个测试用例,第一个如下:
  1. %option noyywrap
  2. %{
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. %}
  7. %%
  8. [^'"\\\n]+/\n   { printf("Matchoo[%s]\n", yytext); }
  9. [^'"\\\n]+      { printf("Matchi2[%s]\n", yytext); }
  10. %%
  11. int main(int argc, char *argv[])
  12. {
  13.     yylex();
  14.     return 0;
  15. }
复制代码
第二个测试用例为
  1. %option noyywrap
  2. %{
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. %}
  7. %%
  8. [^'"\\\n]+/\n   { printf("Matchoo[%s]\n", yytext); }
  9. [^'"\\\n]+      { printf("Matchi2[%s]\n", yytext); }
  10. %%
  11. int main(int argc, char *argv[])
  12. {
  13.     /* Parsing a string. */
  14.     char input[50] = "Linux/$ARCH $KERNELVERSION Kernel Configuration";

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

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

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

  21.     return 0;
  22. }
复制代码
第一个测试用例处理一个来自终端输入的字符串:  "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结尾?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2