B4A Library Verbal Expressions (regex helper)

This is a wrap for this Github project.


VerbalRegEx
Author:
DonManfred (wrapper)
Version: 1
  • VerbalExpression
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • add (pValue As String) As VerbalExpressionWrapper
      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.
      <p/>
      Example: regex().add("\n.*").build() // produce exact "\n.*" regexp
      pValue: - literal expression, not sanitized
      Return type: @return:this builder
    • addModifier (pModifier As Char) As VerbalExpressionWrapper
    • any (value As String) As VerbalExpressionWrapper
      Shortcut to {@link #anyOf(String)}
      value: - CharSequence every char from can be matched
      Return type: @return:this builder
    • anyOf (pValue As String) As VerbalExpressionWrapper
    • anything As VerbalExpressionWrapper
      Add expression that matches anything (includes empty string)
      Return type: @return:this builder
    • anythingBut (pValue As String) As VerbalExpressionWrapper
      Add expression that matches anything, but not passed argument
      pValue: - the string not to match
      Return type: @return:this builder
    • atLeast (from As Int) As VerbalExpressionWrapper
      Produce range count with only minimal number of occurrences for example:
      .find("w").atLeast(1) // produce (?:w){1,}
      from: - minimal number of occurrences
      Return type: @return:this Builder
    • br As VerbalExpressionWrapper
      Shortcut for {@link #lineBreak()}
      Return type: @return:this builder
    • build
    • capt As VerbalExpressionWrapper
      Shortcut for {@link #capture()}
      Return type: @return:this builder
    • capture As VerbalExpressionWrapper
      Adds capture - open brace to current position and closed to suffixes
      Return type: @return:this builder
    • count (count As Int) As VerbalExpressionWrapper
      Add count of previous group for example: .find("w").count(3) // produce -
      (?:w){3}
      count: - number of occurrences of previous group in expression
      Return type: @return:this Builder
    • digit As VerbalExpressionWrapper
      Add same as [0-9]
      Return type: @return:this builder
    • endCapt As VerbalExpressionWrapper
      Shortcut for {@link #endCapture()}
      Return type: @return:this builder
    • endCapture As VerbalExpressionWrapper
      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
      Return type: @return:this builder
    • endGr As VerbalExpressionWrapper
      Closes current unnamed and unmatching group Shortcut for
      {@link #endCapture()} Use it with {@link #group()} for prettify code
      Example: regex().group().maybe("word").count(2).endGr()
      Return type: @return:this builder
    • endOfLine As VerbalExpressionWrapper
      Mark the expression to end at the last character of the line Same as
      {@link #endOfLine(boolean)} with true arg
      Return type: @return:this builder
    • endOfLine2 (pEnable As Boolean) As VerbalExpressionWrapper
      Enable or disable the expression to end at the last character of the line
      pEnable: - enables or disables the line ending
      Return type: @return:this builder
    • find (value As String) As VerbalExpressionWrapper
      Add a string to the expression Syntax sugar for {@link #then(String)} -
      use it in case: regex().find("string") // when it goes first
      value: - the string to be looked for (sanitized)
      Return type: @return:this builder
    • getText (toTest As String) As String
      Extract full string that matches regex Same as
      {@link #getText(String, int)} for 0 group
      toTest: - string to extract from
      Return type: @return:group 0, extracted from text
    • getTextGroups (toTest As String, group As Int) As List
      Extract exact group from string and add it to list
      toTest: - string to extract from
      group: - group to extract
      Return type: @return:list of extracted groups
    • group As VerbalExpressionWrapper
      Same as {@link #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);
      Return type: @return:this builder
    • lineBreak As VerbalExpressionWrapper
      Add universal line break expression
      Return type: @return:this builder
    • maybe (pValue As String) As VerbalExpressionWrapper
      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
      pValue: - the string to be looked for
      Return type: @return:this builder
    • multiple (pValue As String, count() As Int) As VerbalExpressionWrapper
      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)+
      pValue: - the string to be looked for
      count: - (optional) if passed one or two numbers, it used to show count
      or range count
      Return type: @return:this builder
    • nonDigit As VerbalExpressionWrapper
      Add non-digit: [^0-9]
      Return type: @return:this builder
    • nonSpace As VerbalExpressionWrapper
      Add non-whitespace character: [^\s]
      Return type: @return:this builder
    • nonWordChar As VerbalExpressionWrapper
      Add non-word character: [^\w]
      Return type: @return:this builder
    • oneOf (pValues() As String) As VerbalExpressionWrapper
      Adds an alternative expression to be matched based on an array of values
      pValues: - the strings to be looked for
      Return type: @return:this builder
    • oneOrMore As VerbalExpressionWrapper
      Adds "+" char to regexp Same effect as {@link #atLeast(int)} with "1"
      argument Also, used by {@link #multiple(String, int...)} when second
      argument is null, or have length more than 2
      Return type: @return:this builder
    • or (pValue As String) As VerbalExpressionWrapper
      Add a alternative expression to be matched

      Issue #32
      pValue: - the string to be looked for
      Return type: @return:this builder
    • range (pArgs() As String) As VerbalExpressionWrapper
      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]
      pArgs: - pairs for range
      Return type: @return:this builder
    • removeModifier (pModifier As Char) As VerbalExpressionWrapper
    • searchOneLine (pEnable As Boolean) As VerbalExpressionWrapper
    • something As VerbalExpressionWrapper
      Add expression that matches something that might appear once (or more)
      Return type: @return:this builder
    • somethingButNot (pValue As String) As VerbalExpressionWrapper
    • space As VerbalExpressionWrapper
      Add whitespace character, same as [ \t\n\x0B\f\r]
      Return type: @return:this builder
    • startOfLine As VerbalExpressionWrapper
      Mark the expression to start at the beginning of the line Same as
      {@link #startOfLine(boolean)} with true arg
      Return type: @return:this builder
    • startOfLine2 (pEnable As Boolean) As VerbalExpressionWrapper
      Enable or disable the expression to start at the beginning of the line
      pEnable: - enables or disables the line starting
      Return type: @return:this builder
    • tab As VerbalExpressionWrapper
      Add expression to match a tab character (' ')
      Return type: @return:this builder
    • test (pToTest As String) As Boolean
      Test that full string contains regex
      pToTest: - string to check match
      Return type: @return:true if string contains regex, false otherwise
    • testExact (pToTest As String) As Boolean
      Test that full string matches regular expression
      pToTest: - string to check match
      Return type: @return:true if matches exact string, false otherwise
    • then (pValue As String) As VerbalExpressionWrapper
      Add a string to the expression
      pValue: - the string to be looked for (sanitized)
      Return type: @return:this builder
    • toString As String
    • withAnyCase As VerbalExpressionWrapper
      Turn ON matching with ignoring case Example: // matches "a" // matches
      "A" regex().find("a").withAnyCase()
      Return type: @return:this builder
    • word As VerbalExpressionWrapper
      Add word, same as [a-zA-Z_0-9]+
      Return type: @return:this builder
    • wordChar As VerbalExpressionWrapper
      Add word character, same as [a-zA-Z_0-9]
      Return type: @return:this builder
    • zeroOrMore As VerbalExpressionWrapper
      Adds "*" char to regexp, means zero or more times repeated Same effect as
      {@link #atLeast(int)} with "0" argument
      Return type: @return:this builder


Example:
B4X:
Sub Globals
    Dim vr As VerbalExpression
End Sub
Sub Activity_Create(FirstTime As Boolean)

    Dim url As String = "https://www.google.com"

    vr.Initialize
    vr.startOfLine().then("http").maybe("s").then("://").maybe("www.").anythingBut(" ").endOfLine().build()

    Log("URL match = "&vr.testExact(url)) ' prints TRUE
    Log(vr.toString)

End Sub

^(?:http)(?:s)?(?:\:\/\/)(?:www\.)?(?:[^\ ]*)$
 

Attachments

  • libVerbalRegExV1.0.0.zip
    11.4 KB · Views: 389
  • VerbalExpressionsEx.zip
    6.7 KB · Views: 401
Top