开发者

Regex - Get these but not these

开发者 https://www.devze.com 2023-01-31 16:17 出处:网络
开发者_高级运维So I would like a regex which works like this: Get [a-z0-9.-] but NOT \'example\', \'example1\', \'example3\',\'user\',\'home\',\'h3llo\'
开发者_高级运维

So I would like a regex which works like this:

Get [a-z0-9.-] but NOT 'example', 'example1', 'example3','user','home','h3llo'

EDIT

I need this regex in .htaccess

Examples can be anything like those what I wan to get.


You probably need to give more examples of what you want matched, but this is a start: ^(?!example[13]?)[a-z0-9.-]+$

>>> 'Match' if re.match('(?!example[13]?)[a-z0-9.-]', 'example') else 'No match'
'No match'
>>> 'Match' if re.match('(?!example[13]?)[a-z0-9.-]', 'example1') else 'No match'
'No match'
>>> 'Match' if re.match('(?!example[13]?)[a-z0-9.-]', 'example3') else 'No match'
'No match'
>>> 'Match' if re.match('(?!example[13]?)[a-z0-9.-]', 'dsfhdsagfir') else 'No match'
'Match'

It uses negative lookahead to fail on the strings you don't want to match.


How about a RewriteCond? I assume this is for mod_rewrite since you're using an .htaccess file.

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond   %{REQUEST_URI}    !^(example[13]?|user|home|h3llo)$
  RewriteRule   ^([a-z0-9.-]+)$   <make-your-rewrite-using-$1-here>
</IfModule>

[untested]

0

精彩评论

暂无评论...
验证码 换一张
取 消