This class is compatible with B4A, B4i and B4J.
Regex is a very powerful tool for parsing strings. However the regex syntax can be a bit difficult if you are not using regex regularly.
Reading a regex pattern and understanding it is even more challenging.
The purpose of RegexBuilder is to help with building regex patterns. The API is based on a builder pattern.
It is more verbose and hopefully more clear.
It is inspired by VerbalExpressions open source project, though there are many differences between the two.
Lets start with an example. We want to extract the keys and values from a string that looks like:
Second example, validating a hex number:
There are several groups of methods in RegexBuilder:
- Initialize / Clear - Clear the current pattern.
- Append / AppendEscaped - Add a non-capturing group with the given value. The value will be matched.
- AppendAnyOf - A single character out of the list of characters (or character classes) will be matched.
Note that all items in the list are concatenated together.
- AppendAnyBut - Matches any character not in the list of characters.
- AppendAtMostOne / AtLeastOne / ZeroOrMore / Count - Sets the number of expected matches of the last group.
- AppendStartString / EndString - Match the start or end of the string. Use Regex.MULTILINE option to match the start or end of each line.
- StartCapture / EndCapture - Start or end a capturing group. These groups can be later retrieved with Match.Group.
- StartNonCapture / EndNonCapture - Start or end a non-capturing group.
- AppendOr - Adds the or (|) operator.
- Char properties - Common regex characters sets.
- Pattern - Returns the pattern.
- Escape - Escapes the given value and removes any special behavior. It is used by AppendEscaped.
Regex is a very powerful tool for parsing strings. However the regex syntax can be a bit difficult if you are not using regex regularly.
Reading a regex pattern and understanding it is even more challenging.
The purpose of RegexBuilder is to help with building regex patterns. The API is based on a builder pattern.
It is more verbose and hopefully more clear.
It is inspired by VerbalExpressions open source project, though there are many differences between the two.
Lets start with an example. We want to extract the keys and values from a string that looks like:
Key=Value
B4X:
Dim s As String = $"R=Red
G=Green
B=Blue"$
Dim rx As RegexBuilder
rx.Initialize.AppendStartString.StartCapture.Append(rx.CharWord).AppendAtLeastOne.EndCapture 'key
rx.AppendEscaped("=") 'we don't really need to escape this symbol. No harm is done by escaping it.
rx.StartCapture.Append(rx.CharAny).AppendZeroOrMore.EndCapture.AppendEndString 'value
Dim m As Matcher = Regex.Matcher2(rx.Pattern, Regex.MULTILINE, s) 'MULTILINE = StartString and EndString match the beginning and end of each line.
Do While m.Find
Log("Key: " & m.Group(1))
Log("Value: " & m.Group(2))
Loop
Second example, validating a hex number:
B4X:
Dim rx As RegexBuilder
rx.Initialize.Append("0x").AppendAnyOf(Array(rx.CharDigit, "abcdefABCDEF")).AppendAtLeastOne
Log(Regex.IsMatch(rx.Pattern, "0xABcdef123")) 'True
Log(Regex.IsMatch(rx.Pattern, "312123")) 'False
Log(Regex.IsMatch(rx.Pattern, "0xHJK")) 'False
There are several groups of methods in RegexBuilder:
- Initialize / Clear - Clear the current pattern.
- Append / AppendEscaped - Add a non-capturing group with the given value. The value will be matched.
- AppendAnyOf - A single character out of the list of characters (or character classes) will be matched.
Note that all items in the list are concatenated together.
- AppendAnyBut - Matches any character not in the list of characters.
- AppendAtMostOne / AtLeastOne / ZeroOrMore / Count - Sets the number of expected matches of the last group.
- AppendStartString / EndString - Match the start or end of the string. Use Regex.MULTILINE option to match the start or end of each line.
- StartCapture / EndCapture - Start or end a capturing group. These groups can be later retrieved with Match.Group.
- StartNonCapture / EndNonCapture - Start or end a non-capturing group.
- AppendOr - Adds the or (|) operator.
- Char properties - Common regex characters sets.
- Pattern - Returns the pattern.
- Escape - Escapes the given value and removes any special behavior. It is used by AppendEscaped.
B4X:
Dim s As String = $",./;'\=""d23df""$
Log(Regex.IsMatch(rx.Clear.AppendEscaped(s).Pattern, s)) 'true