REGEX rewrite issue to allow hyphen

与世无争的帅哥 提交于 2020-01-05 23:42:18

问题


I have a web service that I have a rewrite on. I need to be able to allow hyphens or dash in the query. Here is my rewrite:

rewrite ^/app/api/sanction/([0-9]+)/athleteList/([a-zA-Z0-9,]+)/-([0-9,]+)$ /app/athleteList.phtml?s=$1&l=$2&c=$3 last;

The query works fine like this:

/app/api/sanction/35172/athleteList/MLEVEL07/25001,24450

However when I put a dash or hyphen here, the query will fail.

/app/api/sanction/35172/athleteList/MLEVEL-07/25001,24450

As you can see, I have the hyphen listed in the regex, unsure of what I may be doing wrong here...


回答1:


You should place the hyphen in the previous group :

([a-zA-Z0-9,-]+)/([0-9,]+)

This will, however, match "---1020-,-02-1-," and i'm not sure this is what you want. Hence my first proposition, corrected now:

([a-zA-Z0-9,]+)(-([0-9]+))?/-([0-9,]+)

This will match only "weofhw234fhweo,sdfsff3284982-20423400" and not "--,j2j,f9223-2-3402--0d-f0s-f"




回答2:


I think you've misplaced the dash. It should be like this

rewrite ^/app/api/sanction/([0-9]+)/athleteList/([a-zA-Z0-9,-]+)/([0-9,]+)$ /app/athleteList.phtml?s=$1&l=$2&c=$3 last;

Note that "-" was moved from after "/" into inside brackets. If dash is the last character inside square brackets, it doesn't have a special meaning to mark a range of characters (like in a-z).




回答3:


Just add a - inside ([a-zA-Z0-9,-]+), at the end of the [] character class, and remove the hyphen after the next /:

rewrite ^/app/api/sanction/([0-9]+)/athleteList/([a-zA-Z0-9,-]+)/([0-9,]+)$ /app/athleteList.phtml?s=$1&l=$2&c=$3 last;
#----------------------------------------------------------^^^^^^^^


来源:https://stackoverflow.com/questions/8882073/regex-rewrite-issue-to-allow-hyphen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!