我在正则表达式方面还很陌生。
输入
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/necklaces
/shop/yellow-gold-necklaces
/shop/white-gold-necklaces
/shop/rose-gold-necklaces
/shop/best-buy-earrings
我使用的正则表达式
\/shop\/[a-z-]*-?earrings
所需结果
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
实际结果
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/best-buy-earrings
我不希望/ shop / best-buy-earrings出现在结果中。请帮助我修复正则表达式。谢谢。
答案 0 :(得分:1)
只需在gold
之前的正则表达式中添加-
并用括号括起来:
\/shop\/([a-z-]*gold-)?earrings
答案 1 :(得分:1)
假设PCRE具有风味,您可以使用:
\/shop\/(?!best-buy)(?:\w+-)*earrings
或者,如果可以使用除斜杠以外的其他定界符:
/shop/(?!best-buy)(?:\w+-)*earrings