RegEx用于匹配Amazon URL中的产品ID

时间:2019-05-04 05:59:25

标签: regex string regex-group regex-greedy

我正在尝试使用Amazon用于产品中网址的“ id”。

我在regex中完全是菜鸟,但到目前为止,在/之后,我正尝试匹配B0。

输入

https://www.amazon.com/gp/product**/B01NAWKYZ0**/ref=ox_sc_act_image_1?smid=ATVPDKIKX0DER&th=1

尝试

\/B0(.{8})

/B01NAWKYZ0匹配,也与斜杠匹配

2 个答案:

答案 0 :(得分:4)

This expression可能会帮助您:

product\/([A-Z0-9]+)\/

enter image description here

此图显示了表达式的工作方式:

enter image description here

您可以简单地使用$1调用所需的输出,()是ID周围的捕获组var str = "https://www.amazon.com/gp/product/B01NAWKYZ0/ref=ox_sc_act_image_1?smid=ATVPDKIKX0DER&th=1"; var regex = /(.+product\/)([A-Z0-9]+)(\/.+)/g; var replace = str.replace(regex, "$2"); console.log(replace);

JavaScript

$string = "https://www.amazon.com/gp/product/B01NAWKYZ0/ref=ox_sc_act_image_1?smid=ATVPDKIKX0DER&th=1";
$regex = '/(.+product\/)([A-Z0-9]+)(\/.+)/s';

$replace = preg_replace($regex, '$2', $string);

print_r($replace);

PHP

# -*- coding: UTF-8 -*-
import re

string = 'https://www.amazon.com/gp/product/B01NAWKYZ0/ref=ox_sc_act_image_1?smid=ATVPDKIKX0DER&th=1'
regex = r'(.+product\/)([A-Z0-9]+)(\/.+)'
matches = re.search(regex, string)
if matches:
    print(matches.group(2)+ " is a match  ")
else: 
    print(' Sorry! No matches! Something is not right!')

Python

B01NAWKYZ0 is a match  

输出

{{1}}

答案 1 :(得分:3)

您的表达式\/B0(.{8})匹配正斜杠本身,后跟B0,然后在组1中捕获匹配除换行符以外的任何字符的8倍的字符。您可以使用例如$1\1

引用该值

这样,如果您想在正斜杠后使用整个值,则该组将不包含B0

使用模式,但该组从B0之前开始,您可以从第一个捕获组中获取整个值:

\/(B0.{8})
  ^

Regex demo

例如:

let str = "https://www.amazon.com/gp/product**/B01NAWKYZ0**/ref=ox_sc_act_image_1?smid=ATVPDKIKX0DER&th=1";
let pattern = /\/(B0.{8})/;
console.log(str.match(pattern)[1]);