Class Regex
- java.lang.Object
-
- com.norconex.commons.lang.text.Regex
-
- All Implemented Interfaces:
IXMLConfigurable
public class Regex extends Object implements IXMLConfigurable
Builder and utility methods making it easier to construct and use regular expressions.
XML configuration usage:
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 ignoreCase="true" dotAll="true"> ^start.*end$ </sampleConfig>The above will match any text that starts with "start" and ends with "ends", regardless if there are new line characters in between.
- Since:
- 2.0.0
- Author:
- Pascal Essiembre
- See Also:
Pattern
-
-
Field Summary
Fields Modifier and Type Field Description static intUNICODE_CASE_INSENSTIVE_FLAGConvenience flag that combinesPattern.UNICODE_CASEandPattern.CASE_INSENSITIVEstatic intUNICODE_MARK_INSENSTIVE_FLAGFlag that ignores diacritical marks when matching or replacing (e.g. accents).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RegexcanonEq()Regexcomments()Patterncompile()Compiles a previously set pattern.Patterncompile(String pattern)Compiles the given pattern without assigning it to this object.static PatterncompileDotAll(String regex, boolean ignoreCase)Compiles a "dotall" pattern (dots match all, including new lines) with optional case sensitivity.RegexFieldValueExtractorcreateKeyValueExtractor()RegexFieldValueExtractorcreateKeyValueExtractor(int keyGroup, int valueGroup)RegexFieldValueExtractorcreateKeyValueExtractor(String key)RegexFieldValueExtractorcreateKeyValueExtractor(String key, int valueGroup)RegexdotAll()booleanequals(Object other)static Stringescape(String pattern)Escape special characters with a backslash (\) in a regular expression.Set<Integer>getFlags()StringgetPattern()inthashCode()RegexignoreCase()RegexignoreDiacritic()Ignores diacritical marks when matching or replacing (e.g. accents).booleanisCanonEq()booleanisComments()booleanisDotAll()booleanisIgnoreCase()booleanisIgnoreDiacritic()booleanisLiteral()booleanisMultiline()booleanisUnicodeCase()booleanisUnicodeCharacterClass()booleanisUnixLines()Regexliteral()voidloadFromXML(XML xml)Load XML configuration values and initialized this object with them.Matchermatcher(CharSequence text)Matches the previously set pattern against the given text.Matchermatcher(String pattern, CharSequence text)Matches the the given pattern against the given text without assigning the pattern to this object.Regexmultiline()voidsaveToXML(XML xml)Saves this object as XML.RegexsetCanonEq(boolean canonEq)RegexsetComments(boolean comments)RegexsetDotAll(boolean dotAll)voidsetFlags(int... flags)RegexsetIgnoreCase(boolean ignoreCase)RegexsetIgnoreDiacritic(boolean ignoreDiacritic)RegexsetLiteral(boolean literal)RegexsetMultiline(boolean multiline)RegexsetPattern(String pattern)RegexsetUnicodeCase(boolean unicode)RegexsetUnicodeCharacterClass(boolean unicode)RegexsetUnixLines(boolean unixLines)StringtoString()RegexunicodeCase()RegexunicodeCharacterClass()RegexunixLines()
-
-
-
Field Detail
-
UNICODE_MARK_INSENSTIVE_FLAG
public static final int UNICODE_MARK_INSENSTIVE_FLAG
Flag that ignores diacritical marks when matching or replacing (e.g. accents). This flag is not supported by JavaPatternand only works when used with this class.- See Also:
- Constant Field Values
-
UNICODE_CASE_INSENSTIVE_FLAG
public static final int UNICODE_CASE_INSENSTIVE_FLAG
Convenience flag that combinesPattern.UNICODE_CASEandPattern.CASE_INSENSITIVE- See Also:
- Constant Field Values
-
-
Method Detail
-
dotAll
public Regex dotAll()
-
setDotAll
public Regex setDotAll(boolean dotAll)
-
isDotAll
public boolean isDotAll()
-
ignoreCase
public Regex ignoreCase()
-
setIgnoreCase
public Regex setIgnoreCase(boolean ignoreCase)
-
isIgnoreCase
public boolean isIgnoreCase()
-
unixLines
public Regex unixLines()
-
setUnixLines
public Regex setUnixLines(boolean unixLines)
-
isUnixLines
public boolean isUnixLines()
-
literal
public Regex literal()
-
setLiteral
public Regex setLiteral(boolean literal)
-
isLiteral
public boolean isLiteral()
-
comments
public Regex comments()
-
setComments
public Regex setComments(boolean comments)
-
isComments
public boolean isComments()
-
multiline
public Regex multiline()
-
setMultiline
public Regex setMultiline(boolean multiline)
-
isMultiline
public boolean isMultiline()
-
canonEq
public Regex canonEq()
-
setCanonEq
public Regex setCanonEq(boolean canonEq)
-
isCanonEq
public boolean isCanonEq()
-
unicodeCase
public Regex unicodeCase()
-
setUnicodeCase
public Regex setUnicodeCase(boolean unicode)
-
isUnicodeCase
public boolean isUnicodeCase()
-
unicodeCharacterClass
public Regex unicodeCharacterClass()
-
setUnicodeCharacterClass
public Regex setUnicodeCharacterClass(boolean unicode)
-
isUnicodeCharacterClass
public boolean isUnicodeCharacterClass()
-
ignoreDiacritic
public Regex ignoreDiacritic()
Ignores diacritical marks when matching or replacing (e.g. accents).- Returns:
- this instance
-
setIgnoreDiacritic
public Regex setIgnoreDiacritic(boolean ignoreDiacritic)
-
isIgnoreDiacritic
public boolean isIgnoreDiacritic()
-
setFlags
public void setFlags(int... flags)
-
getPattern
public String getPattern()
-
compile
public Pattern compile()
Compiles a previously set pattern.
For text-matching with diacritical mark insensitivity support enabled, use
matcher(CharSequence)instead.- Returns:
- compiled pattern
-
compile
public Pattern compile(String pattern)
Compiles the given pattern without assigning it to this object.
For text-matching with diacritical mark insensitivity support enabled, use
matcher(String, CharSequence)instead.- Parameters:
pattern- the pattern to compile- Returns:
- compiled pattern
-
compileDotAll
public static Pattern compileDotAll(String regex, boolean ignoreCase)
Compiles a "dotall" pattern (dots match all, including new lines) with optional case sensitivity.- Parameters:
regex- regular expressionignoreCase-trueto ignore character case.- Returns:
- compiled pattern
-
escape
public static String escape(String pattern)
Escape special characters with a backslash (\) in a regular expression. This is an alternative toPattern.quote(String)for when you do not want the string to be treated as a literal.- Parameters:
pattern- the pattern to escape- Returns:
- escaped pattern
-
matcher
public Matcher matcher(CharSequence text)
Matches the previously set pattern against the given text.- Parameters:
text- the text to match- Returns:
- matcher
-
matcher
public Matcher matcher(String pattern, CharSequence text)
Matches the the given pattern against the given text without assigning the pattern to this object.- Parameters:
pattern- the pattern to matchtext- the text to match- Returns:
- matcher
-
createKeyValueExtractor
public RegexFieldValueExtractor createKeyValueExtractor()
-
createKeyValueExtractor
public RegexFieldValueExtractor createKeyValueExtractor(String key)
-
createKeyValueExtractor
public RegexFieldValueExtractor createKeyValueExtractor(String key, int valueGroup)
-
createKeyValueExtractor
public RegexFieldValueExtractor createKeyValueExtractor(int keyGroup, int valueGroup)
-
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
-
-