Class VerbalExpression.Builder

java.lang.Object
ru.lanwen.verbalregex.VerbalExpression.Builder
Enclosing class:
VerbalExpression

public static class VerbalExpression.Builder extends Object
  • Field Details

  • Constructor Details

  • Method Details

    • sanitize

      private String sanitize(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(String where, 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
    • build

      public VerbalExpression build()
    • add

      public VerbalExpression.Builder add(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

      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
    • startOfLine

      public VerbalExpression.Builder startOfLine()
      Mark the expression to start at the beginning of the line Same as startOfLine(boolean) with true arg
      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
    • endOfLine

      public VerbalExpression.Builder endOfLine()
      Mark the expression to end at the last character of the line Same as endOfLine(boolean) with true arg
      Returns:
      this builder
    • then

      public VerbalExpression.Builder then(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(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(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

      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(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
    • somethingButNot

      public VerbalExpression.Builder somethingButNot(String pValue)
    • lineBreak

      public VerbalExpression.Builder lineBreak()
      Add universal line break expression
      Returns:
      this builder
    • br

      Shortcut for lineBreak()
      Returns:
      this builder
    • tab

      Add expression to match a tab character (' ')
      Returns:
      this builder
    • word

      public VerbalExpression.Builder word()
      Add word, same as [a-zA-Z_0-9]+
      Returns:
      this builder
    • wordChar

      public VerbalExpression.Builder wordChar()
      Add word character, same as [a-zA-Z_0-9]
      Returns:
      this builder
    • nonWordChar

      public VerbalExpression.Builder nonWordChar()
      Add non-word character: [^\w]
      Returns:
      this builder
    • nonDigit

      public VerbalExpression.Builder nonDigit()
      Add non-digit: [^0-9]
      Returns:
      this builder
    • digit

      public VerbalExpression.Builder digit()
      Add same as [0-9]
      Returns:
      this builder
    • space

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

      public VerbalExpression.Builder nonSpace()
      Add non-whitespace character: [^\s]
      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
    • anyOf

      public VerbalExpression.Builder anyOf(String pValue)
    • any

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

      public VerbalExpression.Builder range(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
    • addModifier

      public VerbalExpression.Builder addModifier(char pModifier)
    • removeModifier

      public VerbalExpression.Builder removeModifier(char pModifier)
    • withAnyCase

      public VerbalExpression.Builder withAnyCase(boolean pEnable)
    • withAnyCase

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

      public VerbalExpression.Builder searchOneLine(boolean pEnable)
    • multiple

      public VerbalExpression.Builder multiple(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

      public VerbalExpression.Builder oneOrMore()
      Adds "+" char to regexp Same effect as atLeast(int) with "1" argument Also, used by multiple(String, int...) when second argument is null, or have length more than 2
      Returns:
      this builder
      Since:
      1.2
    • 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:
    • 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:
    • or

      public VerbalExpression.Builder or(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(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(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
    • capt

      public VerbalExpression.Builder capt()
      Shortcut for capture()
      Returns:
      this builder
      Since:
      1.2
    • capt

      public VerbalExpression.Builder capt(String name)
      Shortcut for capture(String)
      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
    • endCapt

      public VerbalExpression.Builder endCapt()
      Shortcut for endCapture()
      Returns:
      this builder
      Since:
      1.2
    • 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