Class 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 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 Java Pattern and only works when used with this class.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Regex

        public Regex()
      • Regex

        public Regex​(String pattern)
      • Regex

        public Regex​(String pattern,
                     int... flags)
    • 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)
      • setPattern

        public Regex setPattern​(String pattern)
      • 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 expression
        ignoreCase - true to 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 to Pattern.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 match
        text - the text to match
        Returns:
        matcher
      • loadFromXML

        public void loadFromXML​(XML xml)
        Description copied from interface: IXMLConfigurable
        Load XML configuration values and initialized this object with them.
        Specified by:
        loadFromXML in interface IXMLConfigurable
        Parameters:
        xml - the XML to load into this object
      • saveToXML

        public void saveToXML​(XML xml)
        Description copied from interface: IXMLConfigurable
        Saves this object as XML.
        Specified by:
        saveToXML in interface IXMLConfigurable
        Parameters:
        xml - the XML that will representing this object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object