PowerBuilder
PowerBuilder
Regular Expression with Multiple Matches and Named Capture Groups
See more Regular Expressions Examples
Demonstrates regular expressions with named capture groups and multiple matches.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sb
integer li_Crlf
string ls_Pattern
oleobject loo_Json
integer li_TimeoutMs
integer li_NumMatches
integer li_Idx_first
integer li_Idx_last
integer i
integer li_MatchCount
li_Success = 0
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_Sb
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Crlf = 1
loo_Sb.AppendLine("Name: John Smith",li_Crlf)
loo_Sb.AppendLine("Name: Jack Johnson",li_Crlf)
loo_Sb.AppendLine("Name: Mary Adams",li_Crlf)
Write-Debug loo_Sb.GetAsString()
// We have the following string:
// Name: John Smith
// Name: Jack Johnson
// Name: Mary Adams
ls_Pattern = "Name:\s+(?<first>\w+)\s+(?<last>\w+)"
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.EmitCompact = 0
li_TimeoutMs = 2000
li_NumMatches = loo_Sb.RegexMatch(ls_Pattern,loo_Json,li_TimeoutMs)
if li_NumMatches < 0 then
// Probably an error in the regular expression.
// Suggestion: Use AI to help create and/or diagnose regular expressions.
Write-Debug loo_Sb.LastErrorText
destroy loo_Sb
destroy loo_Json
return
end if
// Examine the matches:
Write-Debug loo_Json.Emit()
// Here is the JSON showing the matches.
// Important: Capture group 0 always contains the entire match — that is, the portion of the input string that matches the full regular expression.
// {
// "named": {
// "first": 1,
// "last": 2
// },
// "match": [
// {
// "group": [
// {
// "cap": "Name: John Smith",
// "idx": 0,
// "len": 16
// },
// {
// "cap": "John",
// "idx": 6,
// "len": 4
// },
// {
// "cap": "Smith",
// "idx": 11,
// "len": 5
// }
// ]
// },
// {
// "group": [
// {
// "cap": "Name: Jack Johnson",
// "idx": 18,
// "len": 18
// },
// {
// "cap": "Jack",
// "idx": 24,
// "len": 4
// },
// {
// "cap": "Johnson",
// "idx": 29,
// "len": 7
// }
// ]
// },
// {
// "group": [
// {
// "cap": "Name: Mary Adams",
// "idx": 38,
// "len": 16
// },
// {
// "cap": "Mary",
// "idx": 44,
// "len": 4
// },
// {
// "cap": "Adams",
// "idx": 49,
// "len": 5
// }
// ]
// }
// ]
// }
// The capture group index is obtained by looking up the name in the JSON result.
// For example:
li_Idx_first = loo_Json.IntOf("named.first")
li_Idx_last = loo_Json.IntOf("named.last")
i = 0
li_MatchCount = loo_Json.SizeOfArray("match")
do while i < li_MatchCount
Write-Debug "Match " + string(i + 1) + ":"
loo_Json.I = i
loo_Json.J = li_Idx_first
Write-Debug "first: " + loo_Json.StringOf("match[i].group[j].cap")
loo_Json.J = li_Idx_last
Write-Debug "first: " + loo_Json.StringOf("match[i].group[j].cap")
Write-Debug ""
i = i + 1
loop
// Output is:
// Match 1:
// first: John
// first: Smith
//
// Match 2:
// first: Jack
// first: Johnson
//
// Match 3:
// first: Mary
// first: Adams
destroy loo_Sb
destroy loo_Json