开发者

Most-specific matching wild card path

Given a resource structure of Unix-like paths:

e.g.

/foo/bar/baz/phleem/abc.txt

and a set of ant-style wildcard patterns:

*  matches zero or more characters excluding /
** matches zero or more full sub-paths (may not be preceded or followed
   by anything other than /)

All other characters in patterns are literals, and for simplicity's sake, only the characters

A-Z, a-z, 0-9, / and .

are allowed in paths.

Given the above structure:

Am I correct in assuming that of all matching patterns, the most specific match is always the alphabetically last?

Example:

Path:

/foo/bar/baz/phleem/abc.txt

Matching patterns (in alphabetical order):

**               < least specific
**/*.txt
**/phleem/*.txt
/foo/**/abc.txt  < most specific

Update: OK, here's my开发者_开发知识库 definition of "most specific"

a is more specific than b if

  1. the non-wildcard prefix of a is longer than that of b
  2. given two wildcards at the same respective offset, * is more specific than **


Strictly speaking you're not doing an alphabetical ordering since / and * are not letters, so it would be a lexicographical ordering. It appears you're already assuming * < [a-zA-Z0-9./] which is important. Ordering the patterns as such will satisfy your first condition that the non-wildcard prefix is of maximum length. However, the ordering will only ensure that * is used over ** in the first non-equal case; all remaining wildcards become irrelevant to the ordering. This could be a problem. Consider the following two patterns:

/**/*.txt
/*/**

They are ordered, but I would argue the first is actually more specific.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜