无法为其他网址创建正则表达式

时间:2019-05-21 15:11:10

标签: regex jmeter

我是Jmeter的新手,想创建一个可以捕获以下两个URL的正则表达式:

我正在尝试使用以下表达式:

movies/(.+?)/(.+?)&amp

这两个网址都不起作用:

https://in.bookmyshow.com/bengaluru/movies/avengers-endgame/ET00090482&
https://in.bookmyshow.com/movies/the-tashkent-files/ET00069063/&

4 个答案:

答案 0 :(得分:0)

我猜测我们可能会传递一个域并在此处捕获URL的一些组成部分,例如ID。让我们从具有更多边界的表达式开始,然后将它们删除(如果不希望出现的话):

(.+?)(in.bookmyshow.com)\/(.+?)\/([A-Z0-9]+)(.*&)

enter image description here

DEMO

RegEx

如果不需要此表达式,可以在regex101.com中对其进行修改或更改。

RegEx电路

jex.im还有助于可视化表达式。

enter image description here

演示

此代码段只是为了说明捕获组的工作方式:

const regex = /(.+?)(in.bookmyshow.com)\/(.+?)\/([A-Z0-9]+)(.*&)/gm;
const str = `https://in.bookmyshow.com/bengaluru/movies/avengers-endgame/ET00090482&
https://in.bookmyshow.com/movies/the-tashkent-files/ET00069063/&`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

如果我们希望传递包含movies的URL并在没有URL的情况下使它们失败,则可以在初始表达式中添加新的边界:

(.+?)(in.bookmyshow.com)\/(.+)?movies(.+)?\/([A-Z0-9]+)(.*&) 

enter image description here

DEMO

const regex = /(.+?)(in.bookmyshow.com)\/(.+)?movies(.+)?\/([A-Z0-9]+)(.*&)/gm;
const str = `https://in.bookmyshow.com/bengaluru/movies/avengers-endgame/ET00090482&
https://in.bookmyshow.com/movies/the-tashkent-files/ET00069063/&
https://in.bookmyshow.com/music/the-tashkent-files/ET00069063/&
https://in.bookmyshow.com/bengaluru/music/the-tashkent-files/ET00069063/&`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

答案 1 :(得分:0)

我无法发表评论,但想补充Emma发表的内容。

一旦获得所需的分组,JMeter中就会有一个“匹配编号”字段。如果您输入

refName_n_g<group number>

它应该返回您想要的作品。因此,如果正则表达式中的电影标题为第2组,则应放置

refName_n_g2

这是有关JMeter和正则表达式的页面:

Using-RegEx-Regular-Expression-Extractor-with-JMeter

编辑: 使用

movies\/(.+?\/.+?)&amp

您的第1组会给您“ avengers-endgame / ET00090482”

答案 2 :(得分:0)

您的表情:电影/(.+?)/(.+?)& amp

由于您搜索了两个组:电影名称(avengers-endgame)和ID(ET00090482),因此需要在JMeter的正则表达式提取器中将模板设为$ 1 $$ 2 $

此外,请检查以下部分-适用于要检查的字段,以根据要求进行选择

enter image description here

参考:

How to use Regular Expression Extractor in JMeter

答案 3 :(得分:0)

尝试下面的正则表达式,让我知道结果。

电影/(.*?)&

enter image description here

enter image description here enter image description here