Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.1.0+

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

$oSb = ObjCreate("Chilkat.StringBuilder")
Local $bCrlf = True
$oSb.AppendLine("Name: John Smith",$bCrlf)
$oSb.AppendLine("Name: Jack Johnson",$bCrlf)
$oSb.AppendLine("Name: Mary Adams",$bCrlf)

ConsoleWrite($oSb.GetAsString() & @CRLF)

;  We have the following string:
;  Name: John Smith
;  Name: Jack Johnson
;  Name: Mary Adams

Local $sPattern = "Name:\s+(?<first>\w+)\s+(?<last>\w+)"
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.EmitCompact = False

Local $iTimeoutMs = 2000
Local $iNumMatches = $oSb.RegexMatch($sPattern,$oJson,$iTimeoutMs)
If ($iNumMatches < 0) Then
    ;  Probably an error in the regular expression.
    ;  Suggestion: Use AI to help create and/or diagnose regular expressions.
    ConsoleWrite($oSb.LastErrorText & @CRLF)
    Exit
EndIf

;  Examine the matches:
ConsoleWrite($oJson.Emit() & @CRLF)

;  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:

Local $idx_first = $oJson.IntOf("named.first")
Local $idx_last = $oJson.IntOf("named.last")

Local $i = 0
Local $iMatchCount = $oJson.SizeOfArray("match")
While $i < $iMatchCount
    ConsoleWrite("Match " & ($i + 1) & ":" & @CRLF)
    $oJson.I = $i

    $oJson.J = $idx_first
    ConsoleWrite("first: " & $oJson.StringOf("match[i].group[j].cap") & @CRLF)

    $oJson.J = $idx_last
    ConsoleWrite("first: " & $oJson.StringOf("match[i].group[j].cap") & @CRLF)

    ConsoleWrite("" & @CRLF)
    $i = $i + 1
Wend

;  Output is: 

;  Match 1:
;  first: John
;  first: Smith
;  
;  Match 2:
;  first: Jack
;  first: Johnson
;  
;  Match 3:
;  first: Mary
;  first: Adams