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

Swift

func chilkatTest() {
    var success: Bool = false

    let sb = CkoStringBuilder()!
    var crlf: Bool = true
    sb.appendLine(str: "Name: John Smith", crlf: crlf)
    sb.appendLine(str: "Name: Jack Johnson", crlf: crlf)
    sb.appendLine(str: "Name: Mary Adams", crlf: crlf)

    print("\(sb.getAsString()!)")

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

    var pattern: String? = "Name:\\s+(?<first>\\w+)\\s+(?<last>\\w+)"
    let json = CkoJsonObject()!
    json.emitCompact = false

    var timeoutMs: Int = 2000
    var numMatches: Int = sb.regexMatch(pattern: pattern, json: json, timeoutMs: timeoutMs).intValue
    if numMatches < 0 {
        //  Probably an error in the regular expression.
        //  Suggestion: Use AI to help create and/or diagnose regular expressions.
        print("\(sb.lastErrorText!)")
        return
    }

    //  Examine the matches:
    print("\(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:

    var idx_first: Int = json.int(of: "named.first").intValue
    var idx_last: Int = json.int(of: "named.last").intValue

    var i: Int = 0
    var matchCount: Int = json.size(ofArray: "match").intValue
    while i < matchCount {
        print("Match \(i + 1):")
        json.i = i

        json.j = idx_first
        print("first: \(json.string(of: "match[i].group[j].cap")!)")

        json.j = idx_last
        print("first: \(json.string(of: "match[i].group[j].cap")!)")

        print("")
        i = i + 1
    }

    //  Output is: 

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

}