You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改URL路径匹配正则表达式以排除特定格式URL

Solution for Adjusting the Regex to Exclude Specific URLs

Let's break down how to modify your existing regex to exclude the unwanted URLs while keeping the valid matches intact.

Original Regex Recap

Your original pattern matches URLs with regional segments, a post name, and an optional reviews section (including empty reviews paths):

listing/(.+?)/([^\/.]+)/?(reviews/?(.+?)?/?)?/?$

Key Changes Needed

We need to address two exclusion cases:

  • URLs ending with /reviews/ (empty reviews section)
  • URLs containing /add-review/

Modified Regex

Here's the updated pattern that handles both exclusions:

listing/(.+?)/([^\/.]+)/(?!add-review/)(?:reviews/(.+?)/?)?/?$

Breakdown of Modifications

  1. Negative Lookahead for add-review/:
    The (?!add-review/) part is a negative lookahead that checks immediately after the post name's trailing slash. If the next segment is add-review/, the regex will reject the entire URL, directly excluding those unwanted paths.

  2. Enforce Non-Empty Reviews Section:
    We adjusted the reviews segment from reviews/?(.+?)?/? to reviews/(.+?)/? (wrapped in a non-capturing group (?:...)). This ensures that if a reviews/ segment exists, it must be followed by at least one character (no empty reviews paths allowed).

Test Cases

  • Still Matches:
    • listing/united-states/postname1/
    • listing/united-states/california/postname2/
    • listing/united-states/california/chino/postname3/
    • listing/united-states/california/chino/postname4/reviews/testreview/
  • No Longer Matches:
    • listing/united-states/california/chino/postname1/reviews/
    • listing/united-states/california/chino/postname2/add-review/

内容的提问来源于stack exchange,提问作者shamalli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:27:44