k, several things upfront:
** You WILL have to learn precisely how regex works in java. Here is a good tutorial link
regex torial from Sun.
** Note there are some subtle differences between regular expressions in different languages. So, no universal standard.
** Pay particular attention to when you should use Greedy, Reluctant and Possessive quantifiers. It's critically important - means difference
between a working regular expressions and a regular expressions which looks like it SHOULD work, yet it DOESN't -
the most likely reason is in how you use the quantifiers. So learn about them in detail.
** You will need a test harness to try out your regular expressions in an easy way - the tutorial luckly has one for you to download.
It's a good one.
Ok, here are some ideas about how i did it which can help you get started once you learn enough about how regex works in java:
(NOTE: if you don't understand something,don't get stuck, just keep reading - i try to reiterate some important points, so you'll
see me talk about them again)
criteriaFind(String criteria) {
/*PHASE NUMBER 1 (better to put it in a separate function: String [] func1(String criteria) ):
separate the criteria into its constituent elements with "any character" regex prepended and postfixed to them; for example: "criteria=Origin airport='ABC',Destination airport='DFG'" yeilds an array of strings:
.*?Origin airport='ABC'.*?
.*?Destination airport='DFG'.*?
To get this result, you need to come up with a regex which matches a correct criteria input string. If the criteria is wrong, your
regex (if constructed properly) will NOT match it. Note, you will need to use the group() function after that in the Matcher class to obtain all the key='value' pairs. As soon as you get a pair, prepend it and postfix it with .*? - a "any number of any characters" match (we will see why we need it later). So eventually, you will be able to get all of these .*?fieldname='value'.*? (or .*?key='value'.*? as i called it before) and store it in an array of strings. Then func1() returns all of them in a String[].
Basically func1() is using regex (designed by you) to construct a bunch or regex's (those stored in a String[] returned by func1()) we will use to do more matching later.
ok, let's review func1() in brief one more time (i understand, it's very tricky):
1) Create a regex which represents a correct
input for criteria.
2) Try to match your criteria against this regex. If it doesn't match, return new String[0];
This is the most heavyweight regex in this whole deal. Carefully think it through (hint: think of the most general correct value for your criteria).
3) Ok, now the criteria is correct (no weird characters, or wrong format). So use another regex (a much much smaller one) to pick out all the fieldname='value' pairs, prepend and postfix each one of them with .*? and put it in the next available String slot in your String[]. This procedure will use the group() method in the Matcher class.
*/
//do the actual record matching here
synchronized (this) {
//needs to be synchronized to make sure noone attempts to change the reccount by adding or deleting records.
for (every record in the database) {
/*PHASE NUMBER 2: (String func2(DataInfo rec)) puts rec into key1='value1'key2='value2'key3='value3' ..... format and returns it as one large String (let's call it str)*/
for (every one of those .*?key='value'.*? pairs we obtained above) {
/*match it with str. If match is not found break out of this loop to go to the next record. If all the patterns matched a given record, that means all the key='value' pairs in the original criteria were present in this record, so grab this record and put it in some storage (a Vector, maybe)*/
}
}
/*create DataInfo[] out of that storage (the one containing all the matched records) and return it.*/
}
}