Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set sb [new_CkStringBuilder]

set crlf 1
CkStringBuilder_AppendLine $sb "Name: John Smith" $crlf
CkStringBuilder_AppendLine $sb "Name: Jack Johnson" $crlf
CkStringBuilder_AppendLine $sb "Name: Mary Adams" $crlf

puts [CkStringBuilder_getAsString $sb]

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

set pattern "Name:\\s+(?<first>\\w+)\\s+(?<last>\\w+)"
set json [new_CkJsonObject]

CkJsonObject_put_EmitCompact $json 0

set timeoutMs 2000
set numMatches [CkStringBuilder_RegexMatch $sb $pattern $json $timeoutMs]
if {$numMatches < 0} then {
    #  Probably an error in the regular expression.
    #  Suggestion: Use AI to help create and/or diagnose regular expressions.
    puts [CkStringBuilder_lastErrorText $sb]
    delete_CkStringBuilder $sb
    delete_CkJsonObject $json
    exit
}

#  Examine the matches:
puts [CkJsonObject_emit $json]

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

set idx_first [CkJsonObject_IntOf $json "named.first"]
set idx_last [CkJsonObject_IntOf $json "named.last"]

set i 0
set matchCount [CkJsonObject_SizeOfArray $json "match"]
while {$i < $matchCount} {
    puts "Match [expr $i + 1]:"
    CkJsonObject_put_I $json $i

    CkJsonObject_put_J $json $idx_first
    puts "first: [CkJsonObject_stringOf $json {match[i].group[j].cap}]"

    CkJsonObject_put_J $json $idx_last
    puts "first: [CkJsonObject_stringOf $json {match[i].group[j].cap}]"

    puts ""
    set i [expr $i + 1]
}

#  Output is: 

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

delete_CkStringBuilder $sb
delete_CkJsonObject $json