gpt4 book ai didi

java - 网址重写正则表达式 - 不匹配

转载 作者:行者123 更新时间:2023-12-01 14:50:56 25 4
gpt4 key购买 nike

我有这个用于 URL 重写的正则表达式:

<from>^/page/([0-9]+)/order/(.*)/by/(.*)/composition/(.*)/location/(.*)/price_min/(.*)/price_max/(.*)/industry/(.*)/type/(.*)$</from>
<to>/page=$1&order=$2&by=$3&composition=$4&location_id=$5&price_min=$6&price_max=$7&industry_id=$8&type_id=$9</to>

我想匹配如下 URL,但没有匹配到。

/page/2/order/id/by/desc/composition/1/location/none/price_min/2/price_max/2/industry/2/type/3

最佳答案

考虑使用以下一个:

^/page/([0-9]+)/order/(.+?)/by/(.+?)/composition/(.+?)/location/(.+?)/price_min/(.+?)/price_max/(.+?)/industry/(.+?)/type/(.+?)$

正则表达式有两件事:

  1. 您应该使用 .+而不是.* ,因为 / 之间始终存在数据和/
  2. 您应该添加? ,这意味着它将是非贪婪的。相当于写 ([^/]+)相反,这意味着 匹配除 / 之外的任何字符倍数多次

关于java - 网址重写正则表达式 - 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14864513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com