OSRS Plugin API
    Preparing search index...

    Class Regex

    This class is used for the creation and usage of regular expressions in the Lua Plugin API space. This can be used for quickly filtering text for specific text patterns and can be used to quickly capture snippets from a chunk of text.

    This can be used in scenarios such as identifiying strings where a username has appeared, or quickly replacing all instances of a word given you can provide a pattern to the regex object. Regex patterns in this system can handle wild cards, capture groups, and quantifiers.

    To construct a regex object, you must use Regex.new For identifying if a string matches a pattern fully, you can use Regex.match For identifying instances of a pattern within a string, you can use Regex.search For replacing instances of a pattern with a new string, you can use Regex.replace

    Index

    Methods

    • Searches the test string and compares if the full string matches the regular expression.

      Parameters

      • testString: string

        the string to compare the regular expressions against.

      Returns string[]

      a string table with the matched string and any captured sub-expressions, if they have not been disabled.

      --Example Regex
      local exampleRegex = osrs.Regex.new("example")
      --Example using wildcards
      local wildcardRegex = osrs.Regex.new(".*example.*")
      local testStringExact = "example"
      local exampleTable = exampleRegex:match(testStringExact)
      local wildcardTable = wildcardRegex:match(testStringExact)

      -- Both regular expressions should detect 1 match
      osrs.print("Matches: No Wildcard - " .. #exampleTable .. ", With Wildcards - " .. #wildcardTable)

      local testStringLonger = "This is an example of an example sentence"
      local exampleTableLonger = exampleRegex:match(testStringLonger)
      local wildcardTableLonger = wildcardRegex:match(testStringLonger)
      -- The non-wildcard regular expression should not detect a match, but the wildcard regular expression should detect 1 match
      osrs.print("Matches: No Wildcard - " .. #exampleTableLonger .. ", With Wildcards - " .. #wildcardTableLonger)
    • Searches the provided string for all matches of the regular expression and replaces them with the provided replacement string.

      Parameters

      • testString: string

        the string to compare the regular expression against/

      • replacementString: string

        the string to replace all matches of the regular expression with.

      Returns string

      the resulting string with all matches of the regular expression replaced with the replacement string.

      local regexTestSearch = osrs.Regex.new("fail")
      local testString = "This example has failed. If it has failed, all fails should read fail."
      osrs.print("The original string is: " .. testString)
      local replacedString = regexTestSearch:replace(testString, "succeed")
      -- This should print "This example has succeeded. If it has succeeded, all succeed should read succeed."
      osrs.print("The new string is: " .. replacedString)
    • Searches the test string and returns all matches of the regular expression in the provided string.

      Parameters

      • testString: string

        the string to compare the regular expressions against.

      Returns string[]

      a string table with all matched strings and any captured sub-expressions, if they have not been disabled.

      -- Example Regex
      local exampleRegex = osrs.Regex.new("name:")
      -- Example using wildcards
      local exampleWildcardRegex = osrs.Regex.new("name:\\S*")
      local testString = "name:ExampleUser Hi!. name:OtherUser Bye!"

      local exampleTable = exampleRegex:search(testString)
      local exampleWilcardcardTable = exampleWildcardRegex:search(testString)

      -- Will print two elements that are "name:"
      for index, val in ipairs(exampleTable) do
      osrs.print(index .. ": " .. val)
      end

      -- Will print both "name:ExampleUser" and "name:OtherUser"
      for index, val in ipairs(exampleWilcardcardTable) do
      osrs.print(index .. ": " .. val)
      end
    • Static constructor for a regular expression object. This takes a regular expression that is used for its functions, and optional flags for case-insensitivity and whether to ignore subExpressions.

      Parameters

      • pattern: string

        The text of the regular expression.

      • OptionalcaseInsensitive: boolean

        Flag indicating whether the constructed regular expression should treat uppercase and lowercase letters the same.

      • OptionalnoSubExpressions: boolean

        Flag indicating whether the regular expression should not return capture groups

      Returns Regex

      --Example Regex
      local exampleRegex = osrs.Regex.new("example")
      --Example using wildcards
      local wildcardRegex = osrs.Regex.new(".*example.*")

      -- Case Sensitivity
      local caseSensitiveRegex = osrs.Regex.new("example")
      local caseInsensitiveRegex = osrs.Regex.new("example", true)
      local caseString = "Example"

      osrs.print(#caseSensitiveRegex:match(caseString))
      osrs.print(#caseInsensitiveRegex:match(caseString))

      local captureGroupRegex = osrs.Regex.new("(Test)(This)", true, false)
      local ignoreCaptureGroupRegex = osrs.Regex.new("(Test)(This)", true, true)
      local captureExampleString = "TestThis"

      local captureGroupTable = captureGroupRegex:match(captureExampleString)
      local noCaptureGroupTable = ignoreCaptureGroupRegex:match(captureExampleString)

      -- Prints the match: "TestThis", and sub-groups: "Test", and "This"
      for index, val in ipairs(captureGroupTable) do
      osrs.print(index .. ": " .. val)
      end

      -- Prints only the match: "TestThis"
      for index, val in ipairs(noCaptureGroupTable) do
      osrs.print(index .. ": " .. val)
      end