Class VerbalExpression.Builder

  • Enclosing class:
    VerbalExpression

    public static class VerbalExpression.Builder
    extends java.lang.Object
    • Field Detail

      • prefixes

        private java.lang.StringBuilder prefixes
      • source

        private java.lang.StringBuilder source
      • suffixes

        private java.lang.StringBuilder suffixes
      • modifiers

        private int modifiers
      • SYMBOL_MAP

        private static final java.util.Map<java.lang.Character,​java.lang.Integer> SYMBOL_MAP
    • Method Detail

      • sanitize

        private java.lang.String sanitize​(java.lang.String pValue)
        Escapes any non-word char with two backslashes used by any method, except add(String)
        Parameters:
        pValue - - the string for char escaping
        Returns:
        sanitized string value
      • countOccurrencesOf

        private int countOccurrencesOf​(java.lang.String where,
                                       java.lang.String what)
        Counts occurrences of some substring in whole string Same as org.apache.commons.lang3.StringUtils#countMatches(String, java.lang.String) by effect. Used to count braces for or(String) method
        Parameters:
        where - - where to find
        what - - what needs to count matches
        Returns:
        0 if nothing found, count of occurrences instead
      • add

        public VerbalExpression.Builder add​(java.lang.String pValue)
        Append literal expression Everything added to the expression should go trough this method (keep in mind when creating your own methods). All existing methods already use this, so for basic usage, you can just ignore this method.

        Example: regex().add("\n.*").build() // produce exact "\n.*" regexp

        Parameters:
        pValue - - literal expression, not sanitized
        Returns:
        this builder
      • add

        public VerbalExpression.Builder add​(VerbalExpression.Builder regex)
        Append a regex from builder and wrap it with unnamed group (?: ... )
        Parameters:
        regex - - VerbalExpression.Builder, that not changed
        Returns:
        this builder
        Since:
        1.2
      • startOfLine

        public VerbalExpression.Builder startOfLine​(boolean pEnable)
        Enable or disable the expression to start at the beginning of the line
        Parameters:
        pEnable - - enables or disables the line starting
        Returns:
        this builder
      • endOfLine

        public VerbalExpression.Builder endOfLine​(boolean pEnable)
        Enable or disable the expression to end at the last character of the line
        Parameters:
        pEnable - - enables or disables the line ending
        Returns:
        this builder
      • then

        public VerbalExpression.Builder then​(java.lang.String pValue)
        Add a string to the expression
        Parameters:
        pValue - - the string to be looked for (sanitized)
        Returns:
        this builder
      • find

        public VerbalExpression.Builder find​(java.lang.String value)
        Add a string to the expression Syntax sugar for then(String) - use it in case: regex().find("string") // when it goes first
        Parameters:
        value - - the string to be looked for (sanitized)
        Returns:
        this builder
      • maybe

        public VerbalExpression.Builder maybe​(java.lang.String pValue)
        Add a string to the expression that might appear once (or not) Example: The following matches all strings that contain http:// or https:// VerbalExpression regex = regex() .find("http") .maybe("s") .then("://") .anythingBut(" ").build(); regex.test("http://") //true regex.test("https://") //true
        Parameters:
        pValue - - the string to be looked for
        Returns:
        this builder
      • maybe

        public VerbalExpression.Builder maybe​(VerbalExpression.Builder regex)
        Add a regex to the expression that might appear once (or not) Example: The following matches all names that have a prefix or not. VerbalExpression.Builder namePrefix = regex().oneOf("Mr.", "Ms."); VerbalExpression name = regex() .maybe(namePrefix) .space() .zeroOrMore() .word() .oneOrMore() .build(); regex.test("Mr. Bond/") //true regex.test("James") //true
        Parameters:
        regex - - the string to be looked for
        Returns:
        this builder
      • anything

        public VerbalExpression.Builder anything()
        Add expression that matches anything (includes empty string)
        Returns:
        this builder
      • anythingBut

        public VerbalExpression.Builder anythingBut​(java.lang.String pValue)
        Add expression that matches anything, but not passed argument
        Parameters:
        pValue - - the string not to match
        Returns:
        this builder
      • something

        public VerbalExpression.Builder something()
        Add expression that matches something that might appear once (or more)
        Returns:
        this builder
      • space

        public VerbalExpression.Builder space()
        Add whitespace character, same as [ \t\n\x0B\f\r]
        Returns:
        this builder
      • wordBoundary

        public VerbalExpression.Builder wordBoundary()
        Add word boundary: \b

        Example:

        
         VerbalExpression regex = regex()
                 .wordBoundary().find("abc").wordBoundary()
                 .build();
         regex.test("a abc"); // true
         regex.test("a.abc"); // true
         regex.test("aabc"); // false
         
        Returns:
        this builder
      • any

        public VerbalExpression.Builder any​(java.lang.String value)
        Shortcut to anyOf(String)
        Parameters:
        value - - CharSequence every char from can be matched
        Returns:
        this builder
      • range

        public VerbalExpression.Builder range​(java.lang.String... pArgs)
        Add expression to match a range (or multiply ranges) Usage: .range(from, to [, from, to ... ]) Example: The following matches a hexadecimal number: regex().range( "0", "9", "a", "f") // produce [0-9a-f]
        Parameters:
        pArgs - - pairs for range
        Returns:
        this builder
      • withAnyCase

        public VerbalExpression.Builder withAnyCase()
        Turn ON matching with ignoring case Example: // matches "a" // matches "A" regex().find("a").withAnyCase()
        Returns:
        this builder
      • multiple

        public VerbalExpression.Builder multiple​(java.lang.String pValue,
                                                 int... count)
        Convenient method to show that string usage count is exact count, range count or simply one or more Usage: regex().multiply("abc") // Produce (?:abc)+ regex().multiply("abc", null) // Produce (?:abc)+ regex().multiply("abc", (int)from) // Produce (?:abc){from} regex().multiply("abc", (int)from, (int)to) // Produce (?:abc){from, to} regex().multiply("abc", (int)from, (int)to, (int)...) // Produce (?:abc)+
        Parameters:
        pValue - - the string to be looked for
        count - - (optional) if passed one or two numbers, it used to show count or range count
        Returns:
        this builder
        See Also:
        oneOrMore(), then(String), zeroOrMore()
      • zeroOrMore

        public VerbalExpression.Builder zeroOrMore()
        Adds "*" char to regexp, means zero or more times repeated Same effect as atLeast(int) with "0" argument
        Returns:
        this builder
        Since:
        1.2
      • count

        public VerbalExpression.Builder count​(int count)
        Add count of previous group for example: .find("w").count(3) // produce - (?:w){3}
        Parameters:
        count - - number of occurrences of previous group in expression
        Returns:
        this Builder
      • count

        public VerbalExpression.Builder count​(int from,
                                              int to)
        Produce range count for example: .find("w").count(1, 3) // produce (?:w){1,3}
        Parameters:
        from - - minimal number of occurrences
        to - - max number of occurrences
        Returns:
        this Builder
        See Also:
        count(int)
      • atLeast

        public VerbalExpression.Builder atLeast​(int from)
        Produce range count with only minimal number of occurrences for example: .find("w").atLeast(1) // produce (?:w){1,}
        Parameters:
        from - - minimal number of occurrences
        Returns:
        this Builder
        Since:
        1.2
        See Also:
        count(int), oneOrMore(), zeroOrMore()
      • or

        public VerbalExpression.Builder or​(java.lang.String pValue)
        Add a alternative expression to be matched Issue #32
        Parameters:
        pValue - - the string to be looked for
        Returns:
        this builder
      • oneOf

        public VerbalExpression.Builder oneOf​(java.lang.String... pValues)
        Adds an alternative expression to be matched based on an array of values
        Parameters:
        pValues - - the strings to be looked for
        Returns:
        this builder
        Since:
        1.3
      • capture

        public VerbalExpression.Builder capture()
        Adds capture - open brace to current position and closed to suffixes
        Returns:
        this builder
      • capture

        public VerbalExpression.Builder capture​(java.lang.String name)
        Adds named-capture - open brace to current position and closed to suffixes

        Example:
         String text = "test@example.com";
         VerbalExpression regex = regex()
                 .find("@")
                 .capture("domain").anything().build();
         regex.getText(text, "domain"); // => "example.com"
         
        Returns:
        this builder
        Since:
        1.6
      • group

        public VerbalExpression.Builder group()
        Same as capture(), but don't save result May be used to set count of duplicated captures, without creating a new saved capture Example: // Without group() - count(2) applies only to second capture regex().group() .capt().range("0", "1").endCapt().tab() .capt().digit().count(5).endCapt() .endGr().count(2);
        Returns:
        this builder
        Since:
        1.2
      • endCapture

        public VerbalExpression.Builder endCapture()
        Close brace for previous capture and remove last closed brace from suffixes Can be used to continue build regex after capture or to add multiply captures
        Returns:
        this builder
      • endGr

        public VerbalExpression.Builder endGr()
        Closes current unnamed and unmatching group Shortcut for endCapture() Use it with group() for prettify code Example: regex().group().maybe("word").count(2).endGr()
        Returns:
        this builder
        Since:
        1.2