我正在尝试使用Amazon用于产品中网址的“ id”。
我在regex中完全是菜鸟,但到目前为止,在/之后,我正尝试匹配B0。
https://www.amazon.com/gp/product**/B01NAWKYZ0**/ref=ox_sc_act_image_1?smid=ATVPDKIKX0DER&th=1
\/B0(.{8})
与/B01NAWKYZ0
匹配,也与斜杠匹配
答案 0 :(得分:4)
This expression可能会帮助您:
product\/([A-Z0-9]+)\/
此图显示了表达式的工作方式:
您可以简单地使用$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);
。
$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);
# -*- 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!')
B01NAWKYZ0 is a match
{{1}}
答案 1 :(得分:3)
您的表达式\/B0(.{8})
匹配正斜杠本身,后跟B0
,然后在组1中捕获匹配除换行符以外的任何字符的8倍的字符。您可以使用例如$1
或\1
这样,如果您想在正斜杠后使用整个值,则该组将不包含B0
使用模式,但该组从B0
之前开始,您可以从第一个捕获组中获取整个值:
\/(B0.{8})
^
例如:
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]);