nyla.solutions.global.patterns.search.ReLookup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nyla.solutions.global Show documentation
Show all versions of nyla.solutions.global Show documentation
Nyla Solutions Global Java API provides support for basic application
utilities (application configuration, data encryption, debugger and text
processing).
The newest version!
package nyla.solutions.global.patterns.search;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import nyla.solutions.global.util.Text;
/**
*
*
* The RELookup acts as a lookup table where the key is a complex
* regular expression. The expression and values are stored in a hash map.
* Then RELookup.get(argument) method is called,
* RELookup operation will iterate through the given get argument expressions
* looking for a match on the corresponding complex regular expression key.
*
* The value column of the lookup table is used if the regular expression matches
* the argument.
*
* v
*
* ReLookup lookup = new ReLookup();
Assert.assertTrue(lookup instanceof Map);
lookup.put("(001)*.*Green.*${AND}${NOT}.*Blue.*", new FaultError("0001","ERROR"));
lookup.put("(002)*.*Green.*${AND}.*Blue.*", new FaultError("0002","GB"));
lookup.put("(003)*.*Blue.*", new FaultError("0003","BLUE"));
Assert.assertEquals(lookup.get("Green").getCode(), "0001");
Assert.assertEquals(lookup.get("Blue Green").getCode(), "0002");
Assert.assertEquals(lookup.get("Blue with Live of pure").getCode(), "0003");
Complex Regular Expression (And/Not)
By default, regular expressions do not have an easy way to chain expressions together
using AND/NOT logic. The OR logical expression is supported with the character "|". The RELookup
operation combines regular expressions with a special syntax to support AND/NOT logic.
AND Operation
The RELookup supports chaining expressions together with "AND" logic. This is accomplished
by chaining expressions together with “${AND}”
. The string “"{AND}" can be used to separate
two regular expressions. If any of the regular expressions return false then the entire regular
expression is false. In the following example, the regular expression “.*USA.*${AND}.*Greece.*”,
only returns true if the text contains both “USA” and “Greece”.
Complex RE Value Matches
.*USA.*${AND}.*Greece.*
USA and Greece True
.*USA.*${AND}.*Greece.*
USA False
.*USA.*${AND}.*Greece.*
Greece False
.*USA.*${AND}.*Greece.*
Greece USA True
NOT Operation
The RELookup supports negative logic (NOT) for expressions. This is accomplished by prefixing the
expressions with “${NOT}”. In the following example, the regular expression “.*USA.*” only returns true
if the text does not contain the word “USA”. Note that multiple “&{NOT}”(s) can be chained together
with “${AND}”(s) (see table below).
Complex RE Value Matches
${NOT}.*USA.* USA and Greece False
${NOT}.*USA.* USA False
${NOT}.*USA.* Greece True
${NOT}.*USA.* Greece USA False
.*Greece.*${AND}${NOT}.*USA.* ${AND}${NOT}.*Turkey.* Greece Turkey False
.*Greece.*${AND}${NOT}.*USA.* ${AND}${NOT}.*Turkey.* Greece Africa True
* @author Gregory Green
*
*/
public class ReLookup