【发布时间】:2021-07-29 09:20:46
【问题描述】:
我正在尝试从以下元素中清理 url 路径
- ids (1, 14223423, 24fb3bdc-8006-47f0-a608-108f66d20af4)
- 文件名(things.xml、doc.v2.final.csv)
- 域(包含在文件名下)
- 电子邮件 (foo@bar.com)
示例:
/v1/upload/dxxp-sSy449dk_rm_1debit_A_03MAY21.final.csv/email/foo@bar.com?who=knows
期望的结果:
/upload/email
我有一些有用的东西......但我并不自豪(用 Ruby 编写)
# Remove params from the path (everything after the ?)
route = req.path&.split('?')&.first
# Remove filenames with singlular extentions, domains, and emails
route = route&.gsub(/\b[\w-]*@?[\w-]+\.[\w-]+\b/, '')
# Remove ids from the path (any string that contains a number)
route = "/#{route&.scan(/\b[a-z_]+\b/i)&.join('/')}".chomp('/')
我不禁认为这可以简单地用\/([a-z_]+)\/? 之类的东西来完成,但是\/? 太松散了,\/ 太严格了。
【问题讨论】: