Sample code for 30+ languages & platforms
Chilkat2-Python

Regular Expression with Named Capture Groups

See more Regular Expressions Examples

Demonstrates regular expressions with named capture groups.

See the sample code below.

Note: Chilkat uses PCRE2. See PCRE2 Regular Expressions
Also see: PCRE2 Performance

In PCRE2, named capture groups allow you to assign a name to a capturing group, making it easier to reference by name instead of number.

Syntax

(?<name>pattern)

or

(?'name'pattern)

Example

(?<first>\w+)\s+(?<last>\w+)

Applied to:

"John Smith"

Produces:

  • first: John
  • last: Smith

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

subject = "John Smith"
pattern = "(?<first>\\w+)\\s+(?<last>\\w+)"

sb = chilkat2.StringBuilder()
sb.Append(subject)

json = chilkat2.JsonObject()
json.EmitCompact = False

timeoutMs = 2000
numMatches = sb.RegexMatch(pattern,json,timeoutMs)
if (numMatches < 0):
    # Probably an error in the regular expression.
    # Suggestion: Use AI to help create and/or diagnose regular expressions.
    print(sb.LastErrorText)
    sys.exit()

# 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": "John Smith",
#           "idx": 0,
#           "len": 10
#         },
#         {
#           "cap": "John",
#           "idx": 0,
#           "len": 4
#         },
#         {
#           "cap": "Smith",
#           "idx": 5,
#           "len": 5
#         }
#       ]
#     }
#   ]
# }

# The capture group index is obtained by looking up the name in the JSON result.
# For example:

json.I = json.IntOf("named.first")
print("first: " + json.StringOf("match[0].group[i].cap"))

json.I = json.IntOf("named.last")
print("last: " + json.StringOf("match[0].group[i].cap"))

# Output is: 

# first: John
# last: Smith