正则表达式中的逻辑
我正在整理一些正则表达式来处理将传入链接从旧网站重定向到新网站上的等效页面。 我希望我可以在正则表达式中处理以下情况,这样我就不必在后端执行此操作:
传入链接: /reservations/inn_details.asp?num=717
重定向链接: /reservations/property-detail.aspx?pid=00717
如您所见,原始查询字符串值 717 在重定向链接中需要为 00717。 规则是重定向 URL 中的这些 ID 的长度都必须为 5 个字符。
所以我的问题是:是否可以在正则表达式中计算出查询字符串值有多少个字符,然后向其添加足够的前导 0 以等于五个字符?
我可以做四个单独的正则表达式来涵盖值长度为 1、2、3 或 4 位数字的情况,但一次性处理所有这些情况会更酷!
谢谢, B.
I'm putting together some regexps to handle redirecting incoming links from an old site to the equivalent page on the new site. I'm hoping I can handle the following situation in the regexp so I don't have to do it on the back-end:
Incoming link:
/reservations/inn_details.asp?num=717
Redirected link:
/reservations/property-detail.aspx?pid=00717
As you can see, the original query string value of 717 needs to be 00717 in the redirected link. The rule is that these IDs all need to be five characters long in the redirected URL.
So my question is: is it possible within the regexp to figure out how many charactes the query string value is, and then add enough leading 0s to it to equal five characters?
I could do four separate regexps to cover cases where the value is 1, 2, 3 or 4 digits long, but it'd be much cooler do handle it all in one fell swoop!
Thanks,
B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,这不是你能够单独使用正则表达式处理的事情。 在我看来,最简单的解决方法是更改 property-detail.aspx 来进行零填充。
我的意思是,只需将正则表达式重定向到“/reservations/property-detail.aspx?pid=717”,并让 aspx 文件在 pid 前面添加必要的零,然后再获取数据。 无论如何,必须有一些代码来清理该输入(或者至少我希望如此),因此可以轻松地将其添加到该部分中。
Unfortunately this isn't something you'll be able to handle with a regex alone. It seems to me that the easiest fix would be changing property-detail.aspx to do the zero-padding.
What I mean is, just have the regex redirect to "/reservations/property-detail.aspx?pid=717", and have the aspx file add the necessary zeros in front of the pid before it goes off to fetch the data. There has to be some code to sanitize that input anyway (or at least I hope so), so it could be easily added in that section.
如果你想使用 mod_rewrite,有一种方法可以做到:
If you want to use mod_rewrite, there is a way to do that:
无法得知某些随机正则表达式实现中可能会出现什么,但至少对于真正的正则表达式来说,不会。
There's no telling what might be in some random regex implementation, but at least with real regular expressions, no.
您还没有解释您调用正则表达式的通用语言。 然而,大多数语言都提供使用正则表达式来匹配和替换文本的功能。 匹配字符串的数字部分,然后使用您的语言功能用前导零填充数字。
You haven't explained what general-purpose language you're calling the regex from. However most languages provide the facility to use regexes to match and replace text. Match the numeric portion of the string and then use the features of your language to pad the number out with leading zeros.
[?&]num=(\d+)
将为您获取该值。 但正则表达式不实现逻辑,仅实现模式识别。 您的语言或工具集需要提供一种分析和处理它的方法。例如,在 Perl 中,您可能会:
但这当然只是解决您的转换问题之一。
[?&]num=(\d+)
will get the value for you. But regexes do not implement logic, only pattern recognition. Your language or toolset needs to provide a way to analyze and deal with it it.For example in perl, you might:
But that of course just addresses just one of your transformation issues.