URL正则匹配失效求助:需精准匹配example.com下的特定路径
Solution to Exact URL Matching
The problem with your current regex is that it doesn't restrict where the regiony/regiuni pattern appears in the URL—it will match any occurrence of those substrings anywhere in the path, which is why it's catching longer URLs like /obshestvo/regiony-zastavili-ego-zhiti.
To fix this, you need to anchor your regex to match the entire URL path after the domain, ensuring there are no extra segments before or after your target patterns. Here's the corrected regex:
^http://example.com/(regiony|regioni)\/?$
Breakdown of the regex:
^: Anchors the match to the start of the string, ensuring the URL begins exactly withhttp://example.com/.(regiony|regioni): Matches either of your target path segments (swapregioniwithregiunihere if that's the actual pattern you need—your question mentions both, so adjust accordingly).\/?: Makes the trailing slash optional (matches both with and without the slash at the end).$: Anchors the match to the end of the string, ensuring there's nothing after the target segment (no extra path parts, query parameters, etc.).
Testing against your URL list:
- ✅ Matches:
http://example.com/regiony/http://example.com/regioni/http://example.com/regionyhttp://example.com/regioni
- ❌ Doesn't match:
http://example.com/ru/regiony/(has an extra/ru/segment before the target)http://example.com/regiuni/(only if you keptregioniin the regex—swap toregiuniif this should be a match)https://example.com/obshestvo/regiony-zastavili-ego-zhiti(extra path segments and HTTPS instead of HTTP)http://example.com/obshestvo/regiony(extra/obshestvo/segment before the target)
If you need to support HTTPS as well, you can modify the regex to include an optional s:
^https?://example.com/(regiony|regioni)\/?$
Content of the question originates from Stack Exchange, question author Jadasdas
相关产品推荐
相关产品推荐

