Question

I have this regex for an url rewriting:

<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>

I want to match an URL like the below one, but there is no match.

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

Was it helpful?

Solution

Consider using the following one:

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

Two things with your regex:

  1. You should be using .+ instead of .*, since there will always be data between / and /.
  2. You should add ?, which means that it will be non-greedy. It is the equivalent as writing ([^/]+) instead, which means match any character multiple except / multiple times.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top