Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly-typed regex named groups #4

Open
sindresorhus opened this issue May 5, 2021 · 0 comments
Open

Strongly-typed regex named groups #4

sindresorhus opened this issue May 5, 2021 · 0 comments

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented May 5, 2021

Just an idea (untested):

struct Result: Codable {
	let word: String?
	let number: Int?
}

let regex = Regex(#"\d+(?<\#(.word)>[a-z]+)(?<\#(.number)>\d+)"#, type: Result.self)

let groups = regex.firstMatch(in: "123unicorn456")?.namedGroups

print(groups.word)
//=> Optional("unicorn")

print(groups.number)
//=> Optional(456)
//             ^ Int

Feedback wanted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant