Assuming the rules are the same as you said: literal or begin with *.
Java:
public static boolean matches(String candidate, List<String> rules) { for(String rule : rules) { if (rule.startsWith("*")) { rule = rule.substring(2); } if (candidate.endsWith(rule)) { return true; } } return false; }
It scales to the number of rules you have.
EDIT:
Just to be understood here.
When I say "sort the rules", I really want to create a tree from the characters of the rule.
Then you use the match string to try and traverse the tree (i.e. if I have the string xyz, I start with the x character and see if it has a y branch, and then a z-element).
For “wildcards,” I use the same concept, but fill it in “back” and go through the back of the candidate.
If you have LOT (LOT LOT) rules, I would sort the rules.
For asymmetric matches, you repeat for each character the narrowing of the possible rules (i.e. if it starts with "w", then you work with the rules of "w", etc.)
If it is a wildcard, you do the same, but you work against the list of "reverse rules" and simply match the end of the line to the end of the rule.
Will hartung
source share