V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
trlsmax
V2EX  ›  问与答

VSCode task.json 里的 ProblemMatcher 正则编写请教

  •  
  •   trlsmax · 2018-05-23 13:48:53 +08:00 · 2219 次点击
    这是一个创建于 2136 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大神好, 我现在有个需求是用 VSCode 编写 51 单片机的程序,编译器用的是 keil c51。现在希望编译报错时 VSCode 能识别,参考网上的例子自己改了个 task.json。

    编译输出是这样的

    [Compiling main.c ...]
    
    c51 compiler v9.00 - sn: t1amc-bzmwsc
    copyright keil elektronik gmbh 1987 - 2009
    *** warning c500 in line 1 of main.c: license error (r208: renew license id code (lic))
    
    *** error c141 in line 170 of main.c: syntax error near 'display_num'
    
    c51 compilation complete.  1 warning(s),  1 error(s)
    

    在官方推荐的网站 https://regex101.com/’,用以下规则,已经可以匹配到错误

    ^\*\*\* (error|warning) c\d+ in line (\d+) of (.*): (.*)$
    

    但这个规则放到 task.json 里是报错的,要怎么写? 这样?

    "regexp": "^//*//*//* (error|warning) c\\d+ in line (\\d+) of (.*): (.*)$",
    "file": 3,
    "line": 2,
    "severity": 1,
    "message": 4
    

    但这样是不行的

    第 1 条附言  ·  2018-05-24 10:18:22 +08:00
    这样写就可以了,是自己不会正则
    "regexp": "^//*//*//* (error|warning) c\\d+ in line (\\d+) of (.*?): (.*)$",
    2 条回复    2018-05-23 14:24:08 +08:00
    golmic
        1
    golmic  
       2018-05-23 13:56:52 +08:00 via Android
    我觉得,你可以发原始文本和想匹配的文本。然后我可以看看怎么写正则。这个问题感觉和 vscode 无关
    trlsmax
        2
    trlsmax  
    OP
       2018-05-23 14:24:08 +08:00
    原始文本
    ```
    [Compiling main.c ...]

    c51 compiler v9.00 - sn: t1amc-bzmwsc
    copyright keil elektronik gmbh 1987 - 2009
    *** warning c500 in line 1 of main.c: license error (r208: renew license id code (lic))

    *** error c141 in line 170 of main.c: syntax error near 'display_num'

    c51 compilation complete. 1 warning(s), 1 error(s)
    ```

    要匹配的文本
    ```
    *** error c141 in line 170 of main.c: syntax error near 'display_num'
    ```
    其中,要提取的是 error, 在行 170,文件 main.c,错误信息 syntax error near 'display_num'

    我现在用的是
    ```
    ^\*\*\* (error|warning) c\d+ in line (\d+) of (.*): (.*)$
    ```
    https://regex101.com 是测试 OK 的,就是在 VSCode 里不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1104 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 18:50 · PVG 02:50 · LAX 11:50 · JFK 14:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.