B4J Programming Press on the image to return to the main documentation page.

Regular Expressions (Core)

List of types:

Matcher
Regex

Matcher

A Matcher object is used to search for a pattern in a string.
Regex.Matcher returns a matcher object for a specific pattern and specific text.
Example:
Dim text, pattern As String
text = "This is an interesting sentence with two numbers: 123456 and 7890."
pattern = "\d+" 'one or more digits
Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, text)
Do While Matcher1.Find
  Log("Found: " & Matcher1.Match)
Loop

Events:

None

Members:


  Find As Boolean

  GetEnd (Index As Int) As Int

  GetStart (Index As Int) As Int

  Group (Index As Int) As String

  GroupCount As Int [read only]

  IsInitialized As Boolean

  Match As String [read only]

Members description:

Find As Boolean
Searches for the next substring that matches the pattern.
Returns True is such a match was found.
Example:
Dim text, pattern As String
text = "This is an interesting sentence with two numbers: 123456 and 7890."
pattern = "\d+" 'one or more digits
Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, text)
Do While Matcher1.Find
  Log("Found: " & Matcher1.Match)
Loop
GetEnd (Index As Int) As Int
Returns the end offset of the specified captured group.
Use GetEnd(0) to get the end offset of the whole match.
GetStart (Index As Int) As Int
Returns the start offset of the specified captured group.
Use GetStart(0) to get the start offset of the whole match.
Group (Index As Int) As String
Returns the value of the specified captured group.
Group(0) returns the whole match.
GroupCount As Int [read only]
Returns the number of capturing groups in the pattern.
Note that the number returned does not include group(0) which is the whole match.
IsInitialized As Boolean
Match As String [read only]
Returns the matched value. This is the same as calling Group(0).

Regex

Regex is a predefined object that contains regular expressions methods.
All methods receive a 'pattern' string. This is the regular expression pattern.
More information about the regular expression engine can be found here: Pattern Javadoc
Regular expression in Basic4android tutorial.

Events:

None

Members:


  CASE_INSENSITIVE As Int

  IsMatch (Pattern As String, Text As String) As Boolean

  IsMatch2 (Pattern As String, Options As Int, Text As String) As Boolean

  Matcher (Pattern As String, Text As String) As Matcher

  Matcher2 (Pattern As String, Options As Int, Text As String) As Matcher

  MULTILINE As Int

  Replace (Pattern As String, Text As String, Template As String) As String

  Replace2 (Pattern As String, Options As Int, Text As String, Template As String) As String

  Split (Pattern As String, Text As String) As String()

  Split2 (Pattern As String, Options As Int, Text As String) As String()

Members description:

CASE_INSENSITIVE As Int
Enables case insensitive matching.
IsMatch (Pattern As String, Text As String) As Boolean
Tests whether the given text is a match for the given pattern.
The whole text should match the pattern. Use Matcher if you look for a substring that matches the pattern.
Example:
If Regex.IsMatch("\d\d\d", EditText1.Text) = False Then ...
IsMatch2 (Pattern As String, Options As Int, Text As String) As Boolean
Tests whether the given text is a match for the given pattern.
Options - One or more pattern options. These options can be combined with Bit.Or.
Matcher (Pattern As String, Text As String) As Matcher
Returns a Matcher object which can be used to find matches of the given pattern in the text.
Example:
Dim text, pattern As String
text = "This is an interesting sentence with two numbers: 123456 and 7890."
pattern = "\d+" 'one or more digits
Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, text)
Do While Matcher1.Find
  Log("Found: " & Matcher1.Match)
Loop
Matcher2 (Pattern As String, Options As Int, Text As String) As Matcher
Same as Matcher with the additional pattern options.
MULTILINE As Int
Changes ^ and $ to match the start and end of each line instead of the whole string.
Replace (Pattern As String, Text As String, Template As String) As String
Replaces all the matches in the text based on the specified pattern and template.
Example:
Log(Regex.Replace("\d", "1 2 3 4", "-$0-")) '-1- -2- -3- -4-
Replace2 (Pattern As String, Options As Int, Text As String, Template As String) As String
Similar to Replace. Allows setting the regex options.
Split (Pattern As String, Text As String) As String()
Splits the given text around matches of the pattern.
Note that trailing empty matches will be removed.
Example:
Dim components() As String
components = Regex.Split(",", "abc,def,,ghi") 'returns: "abc", "def", "", "ghi"
Split2 (Pattern As String, Options As Int, Text As String) As String()
Same as Split with the additional pattern options.
Top