所以我一直在尝试过去3个小时找到这个问题的解决方案。最后在StackOverflow上发布这个,因为我找不到任何解决方案。问题是我有
形式的URL/myurlbase/firstname-lastname-nameId
firstname是一个字符串 lastname是一个字符串,这是可选的(所以url可以是/ myurlbase / firstname-nameId) nameID是一个数字
我希望正则表达式匹配只有那些名字,其中firstname和lastname中没有任何特殊字符。但名称可以有外来名称重音字符。(UTF-8编码)
我将它用作.htaccess规则,因此表达式必须匹配。请不要回答一个与其他方式匹配的答案并将其放在前面。
答案 0 :(得分:0)
您可以使用此重写规则:
RewriteRule ^myurlbase/([a-z]+)-(?:([a-z]+)-)?([0-9]+)/?$ /target?fn=$1&ln=$2&id=$3 [L,NC]
答案 1 :(得分:0)
(?=\b\/(\w+)-(\w+)\b-.*?$)
RewriteRule ^myurlbase/(?=\b\/(\w+)-(\w+)\b-.*?$) /page.php?fn=$1&ln=$2 [L,NC]
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=\b\/(\w+)-(\w+)\b-.*?$)»
Assert position at a word boundary «\b»
Match the character “/” literally «\/»
Match the regular expression below and capture its match into backreference number 1 «(\w+)»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “-” literally «-»
Match the regular expression below and capture its match into backreference number 2 «(\w+)»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at a word boundary «\b»
Match the character “-” literally «-»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»