【问题标题】:Yet another regex - how to identify a querystring另一个正则表达式 - 如何识别查询字符串
【发布时间】:2012-02-04 02:56:16
【问题描述】:

我正在使用 urlrewriting.net 进行 urlrewriting。我需要一些关于正则表达式的帮助(我仍然没有得到......)。

我想匹配

  • www.mysite.com/restaurant -> 匹配并返回 "restaurant"
  • www.mysite.com/restaurant?page=1 -> 匹配和 返回“restaurant
  • www.mysite.com/restaurant?[SOME_RANDOM_QUERYSTRING] -> 匹配 并返回“restaurant
  • www.mysite.com/seattle/restaurant -> 匹配并返回“seattle”和 "restaurant"
  • www.mysite.com/seattle/restaurant?page=1 -> 匹配和 返回“seattle”和“restaurant
  • www.mysite.com/seattle/restaurant?[SOME_RANDOM_QUERYSTRING] -> 匹配 并返回“seattle”和“restaurant
  • www.mysite.com/seattle/restaurant-michelangelo -> 不要抓到
  • www.mysite.com/seattle/restaurant/sushi -> 匹配并返回“seattle” 和“restaurant”和“sushi
  • www.mysite.com/seattle/restaurant/sushi?page=1 -> 匹配并返回 “seattle”和“restaurant”和“sushi
  • www.mysite.com/seattle/restaurant/sushi?[SOME_RANDOM_QUERYSTRING] -> 匹配并返回“seattle”和“restaurant”和“sushi
  • www.mysite.com/seattle/restaurant-michelangelo -> 不要抓到

关键是我需要 url 的目录部分而不是查询字符串部分。问题是我可以从我的网络分析工具中看到,人们用两个词进行搜索。他们都搜索城市(西雅图)+类别(餐厅),例如。 “西雅图餐厅”,也适用于城市 (seattle) + 餐厅名称 (restaurant-michelangelo),例如。 “西雅图餐厅-米开朗基罗”。从结构的角度来看,这当然是一团糟,因为这不是层次结构。在理想的世界中,层次结构是城市 -> 类别 -> 餐厅。但我仍然想在我的 url 结构中适应这种搜索行为。同时我也有一个页面列出了全国所有的餐馆。

我想获得有关如何创建正则表达式以及创建它们的最有效方法的帮助,因为我猜它们可能会变得非常昂贵。

谢谢

托马斯

【问题讨论】:

  • 您如何区分类别和名称?所有的名字都有破折号还是什么?

标签: regex url-rewriting regex-greedy urlrewriting.net


【解决方案1】:

使用这个:

/\/[A-Za-z0-9]{1,}(?:\/|$|\?)/

匹配/字母数字 1-无穷大,然后是斜线、行尾或问号

【讨论】:

  • 嗯...我无法让它工作。我没有那么多类别,所以也许我应该做一些类似 ^~/(.*)/restaurant、^~/(.*)/café 等的事情。然后我需要删除任何查询字符串(如果它们存在) )。那么,无论查询字符串中的参数数量如何,正则表达式会如何删除查询字符串(如果存在)?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2023-02-03
相关资源
最近更新 更多