mirror of
https://github.com/apple/swift.git
synced 2026-06-20 15:42:51 +02:00
9ead8781d4
Co-authored-by: Alex Martini <amartini@apple.com> Co-authored-by: Graham Lee <glee23@apple.com> Co-authored-by: Chris Adamson <cadamson@apple.com> Co-authored-by: Kirby Turner <kirby_turner@apple.com> Co-authored-by: Paris Pinkney <ppinkney@apple.com> Co-authored-by: Dave Spector <dspector@apple.com> Co-authored-by: Sofia Rodriguez Morales <sofia_rodriguez@apple.com> Co-authored-by: Ethan Kusters <ekusters@apple.com> Co-authored-by: Goli Mohammadi <g_mohammadi@apple.com> Co-authored-by: Adora Vaz <a_vaz@apple.com> Co-authored-by: David Rönnqvist <ronnqvist@apple.com> Co-authored-by: Nate Merseth Cook <natecook@apple.com> Co-authored-by: Susan Conant <susan_c@apple.com>
1.5 KiB
1.5 KiB
RegexBuilder
Use an expressive domain-specific language to build regular expressions, for operations like searching and replacing in text.
Overview
Regular expressions, also known as regexes, are a powerful tool for matching patterns in text. Swift supports several ways to create a regular expression, including from a string, as a literal, and using this DSL. For example:
let word = OneOrMore(.word)
let emailPattern = Regex {
Capture {
ZeroOrMore {
word
"."
}
word
}
"@"
Capture {
word
OneOrMore {
"."
word
}
}
}
let text = "My email is my.name@example.com."
if let match = text.firstMatch(of: emailPattern) {
let (wholeMatch, name, domain) = match.output
// wholeMatch is "my.name@example.com"
// name is "my.name"
// domain is "example.com"
}
Topics
Components
Swift/RegexComponentRegexBuilder/CharacterClassRegexBuilder/AnchorRegexBuilder/LookaheadRegexBuilder/NegativeLookaheadRegexBuilder/ChoiceOf
Quantifiers
RegexBuilder/OneRegexBuilder/OptionallyRegexBuilder/ZeroOrMoreRegexBuilder/OneOrMoreRegexBuilder/RepeatRegexBuilder/Local
Captures
RegexBuilder/CaptureRegexBuilder/TryCaptureRegexBuilder/Reference
Builders
RegexBuilder/RegexComponentBuilderRegexBuilder/AlternationBuilder