RegEx 用于在多行中以特定顺序匹配单词序列

问题描述 投票:0回答:0

问题陈述:我想检查“scope.APIName.functionName()”是否包含在 Try/Catch 块中以捕获错误。

描述:

我正在尝试生成正则表达式,它将在多行代码中搜索“try{”后跟“scope.APIName.functionName”后跟“catch(”模式。它应该忽略这些模式之间的任何东西。这里在下面提到的示例中,此正则表达式应首先查找“try{”模式,然后查找“scope.APIName.functionName”模式,最后查找“catch (”。

try {
​
        // Execute Data Stream Action. 
        var stream = scope.APIName.functionName('Data stream');
​
        // Process each item in the data stream
        while (stream.hasNext()) {
​
            // Get a single item from the data stream.
            var user = stream.next();
        
            // Only log the first item in each page
            if (stream.getItemInPageIndex() == 0) {
                gs.info('first user on page is ' + user.name);
            }
        }       
    } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    } finally {
        stream.close();
    }

我试过了:

/^try\{(?:\n(?!(?: +catch|}\))).*)*\n +catch(.*)\n}\)$/gm
我必须将此正则表达式与 Javascript 一起使用。我正在使用 https://regex101.com/ 进行测试。

但是没用。

javascript regex regex-lookarounds regex-negation
© www.soinside.com 2019 - 2024. All rights reserved.