Conditions:
-
there are many rules ,maybe hundreds, which are like :
{aab*, aabc*,
aabcdd*, dtctddds*,
*ddt*,
*cddt*,
*bcddt*,
*t,
*ttt,
*ccddttt} -
each time I will get one string, then I should find the longest matched rule.
Examples:
example 1.string is aabcddttt the matched rule should be: aabcdd*
example 2. string is accddttt the matched rule should be *ccddttt
Question:
I don’t want to use the rules in a long array to match the string one by one,that is inefficient method.maybe I should use the string as a regex to match the hundred rules.But yet I can’t find a elegant way to solve this problem.
- Can I use some regexes to get the result?
- Which is the best/fastest way to match?
Java, plain C or shell are preferred,please don’t use C++ STL
You could try matching them all at once with a brackets around each sub-rule. You could use the group to determine which matched.
prints