crossroadlabs / Regex

Regular expressions for swift
Apache License 2.0
332 stars 33 forks source link

Feature Request: Support in-pattern named groups #53

Open KyLeggiero opened 3 years ago

KyLeggiero commented 3 years ago

I'd love to have this support the same named groups that NSRE supports, like this:

// NSRegularExpression
try! NSRegularExpression(pattern:
        "^(?<major>\\d+)\\." +
        "(?<minor>\\d+)\\." +
        "(?<patch>\\d+)" +
        "(?:-(?<preRelease>(?:(?<preReleaseId>[0-9A-Za-z-]+)\\.?)+))?" +
        "(?:\\+(?<build>(?:(?<buildId>[0-9A-Za-z-]+)\\.?)+))?$",
        options: [])

// Crossroad Labs Regex
try! Regex(pattern:
        "^(?<major>\\d+)\\." +
        "(?<minor>\\d+)\\." +
        "(?<patch>\\d+)" +
        "(?:-(?<preRelease>(?:(?<preReleaseId>[0-9A-Za-z-]+)\\.?)+))?" +
        "(?:\\+(?<build>(?:(?<buildId>[0-9A-Za-z-]+)\\.?)+))?$",
        groupNames: "major", "minor", "patch", "preRelease", "preReleaseId", "build", "buildId")

This feels fragile to me; I feel like it might easily slip so that one group's name doesn't represent the intended group, and that might be subtle enough to not catch in testing.

It's also silly because it accepts the same syntax for in-pattern group names that NSRegularExpression accepts, without using it.