开发者

regexr how to exclude a string match

开发者 https://www.devze.com 2023-04-06 11:38 出处:网络
I want to match the following: any string which contains any character, except / or . this is d开发者_StackOverflow中文版one using ([^/\\.]+), how ever, I also want to add a string \"faq\" so to matc

I want to match the following:

any string which contains any character, except / or . this is d开发者_StackOverflow中文版one using ([^/\.]+), how ever, I also want to add a string "faq" so to match any character, except if it's a string of "faq", some examples:

Currently, my Regex:

^([^/\.]+)/([^/\.]+)/$

However, it captures "faq" aswell.

faq/fsdfsd/ => No match
faqgff/fdsfs/ => Match
jhpo/ijkd/ => Match
f/iout/ => Match
a/iout/ => Match
q/iout/ => Match

I've tried running ([^/.]+)

Yes, it's for .htaccess redirects :)


This would be a negative lookhead

(?!.*faq)

it is true when it does not find the pattern "faq"

So include it in your regex like this

^(?!.*faq)([^/\.]+)/([^/\.]+)/$

Then it will match for your pattern, but fail if the string includes "faq". (Hope I understood you correctly)

See it here on Regexr


I can't say I fully understand your problem statement:

want to add a string "faq" so to match any character, except if it's a string og "faq"

However, it sounds like negative lookbehind might be the tool for the job: https://serverfault.com/questions/78624/apache-2-negative-filesmatch-filesnotmatch

0

精彩评论

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