Class RegexFieldValueExtractor
- java.lang.Object
-
- com.norconex.commons.lang.text.RegexFieldValueExtractor
-
- All Implemented Interfaces:
IXMLConfigurable
public class RegexFieldValueExtractor extends Object implements IXMLConfigurable
Simplify extraction of field/value pairs (or "key/value") from text using regular expression. Match groups can be used to identify the fields and values. Field matching is optional and can be set explicitly instead. If both a "toField" and a "fieldGroup" are provided, the toField act as a default when no fields could be obtained from matching. At least one of "toField" or "fieldGroup" must be specified. If fieldGroup is specified without a "toField" and finds no matches, the matching of the value is ignored. If no value group is provided, it assumes the entire regex match is the value. If more than one value is extracted for a given toField, they will be available as a list.
When initialized with a "pattern" only instead of passing or configuring a
Regexinstance, a default one will be created, assuming case insensitivity and dots matching any character.XML configuration usage:
toField="(toField name)" fieldGroup="(toField name match group index)" valueGroup="(value match group index)" onSet="[append|prepend|replace|optional]" ignoreCase="[false|true]" ignoreDiacritic="[false|true]" dotAll="[false|true]" unixLines="[false|true]" literal="[false|true]" comments="[false|true]" multiline="[false|true]" canonEq="[false|true]" unicodeCase="[false|true]" unicodeCharacterClass="[false|true]"The above are configurable attributes consuming classes can expect. The actual regular expression is expected to be the tag content. Many of the available attributes on XML configuration represent the regular expression flags as defined in
Pattern.XML usage example:
<sampleConfig fieldGroup="1" valueGroup="2"> (DocNo):(\d+) </sampleConfig>The above is configured to extract "DocNo" as the toField, and the following numeric characters will make up the value.
- Since:
- 2.0.0 (moved from Norconex Importer RegexKeyValueExtractor)
- Author:
- Pascal Essiembre
-
-
Field Summary
Fields Modifier and Type Field Description static RegexFieldValueExtractor[]EMPTY_ARRAY
-
Constructor Summary
Constructors Constructor Description RegexFieldValueExtractor()RegexFieldValueExtractor(Regex regex)RegexFieldValueExtractor(Regex regex, int fieldGroup, int valueGroup)RegexFieldValueExtractor(Regex regex, String field)RegexFieldValueExtractor(Regex regex, String field, int valueGroup)RegexFieldValueExtractor(String pattern)RegexFieldValueExtractor(String pattern, int fieldGroup, int valueGroup)RegexFieldValueExtractor(String pattern, String field)RegexFieldValueExtractor(String pattern, String field, int valueGroup)
-
Method Summary
-
-
-
Field Detail
-
EMPTY_ARRAY
public static final RegexFieldValueExtractor[] EMPTY_ARRAY
-
-
Constructor Detail
-
RegexFieldValueExtractor
public RegexFieldValueExtractor()
-
RegexFieldValueExtractor
public RegexFieldValueExtractor(String pattern)
-
RegexFieldValueExtractor
public RegexFieldValueExtractor(String pattern, String field, int valueGroup)
-
RegexFieldValueExtractor
public RegexFieldValueExtractor(String pattern, int fieldGroup, int valueGroup)
-
RegexFieldValueExtractor
public RegexFieldValueExtractor(Regex regex)
-
RegexFieldValueExtractor
public RegexFieldValueExtractor(Regex regex, String field, int valueGroup)
-
RegexFieldValueExtractor
public RegexFieldValueExtractor(Regex regex, int fieldGroup, int valueGroup)
-
-
Method Detail
-
getRegex
public Regex getRegex()
-
setRegex
public RegexFieldValueExtractor setRegex(Regex regex)
-
getFieldGroup
public int getFieldGroup()
-
setFieldGroup
public RegexFieldValueExtractor setFieldGroup(int fieldGroup)
-
getValueGroup
public int getValueGroup()
-
setValueGroup
public RegexFieldValueExtractor setValueGroup(int valueGroup)
-
getToField
public String getToField()
-
setToField
public RegexFieldValueExtractor setToField(String field)
-
getOnSet
public PropertySetter getOnSet()
Gets the property setter to use when a value is set.- Returns:
- property setter
- Since:
- 3.0.0
-
setOnSet
public RegexFieldValueExtractor setOnSet(PropertySetter onSet)
Sets the property setter to use when a value is set.- Parameters:
onSet- property setter- Returns:
- this instance
- Since:
- 3.0.0
-
extractFieldValues
public void extractFieldValues(Properties dest, CharSequence text)
-
extractFieldValues
public Properties extractFieldValues(CharSequence text)
-
extractFieldValues
public static void extractFieldValues(Properties dest, CharSequence text, List<RegexFieldValueExtractor> extractors)
-
extractFieldValues
public static Properties extractFieldValues(CharSequence text, List<RegexFieldValueExtractor> extractors)
-
extractFieldValues
public static void extractFieldValues(Properties dest, CharSequence text, RegexFieldValueExtractor... extractors)
-
extractFieldValues
public static Properties extractFieldValues(CharSequence text, RegexFieldValueExtractor... extractors)
-
loadFromXML
public void loadFromXML(XML xml)
Description copied from interface:IXMLConfigurableLoad XML configuration values and initialized this object with them.- Specified by:
loadFromXMLin interfaceIXMLConfigurable- Parameters:
xml- the XML to load into this object
-
saveToXML
public void saveToXML(XML xml)
Description copied from interface:IXMLConfigurableSaves this object as XML.- Specified by:
saveToXMLin interfaceIXMLConfigurable- Parameters:
xml- the XML that will representing this object
-
-