mirror of
https://github.com/apple/swift.git
synced 2026-10-24 20:15:56 +02:00
+3








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>
73 lines
1.5 KiB
Markdown
73 lines
1.5 KiB
Markdown
# 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:
|
|
|
|
```swift
|
|
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/RegexComponent``
|
|
- ``RegexBuilder/CharacterClass``
|
|
- ``RegexBuilder/Anchor``
|
|
- ``RegexBuilder/Lookahead``
|
|
- ``RegexBuilder/NegativeLookahead``
|
|
- ``RegexBuilder/ChoiceOf``
|
|
|
|
### Quantifiers
|
|
|
|
- ``RegexBuilder/One``
|
|
- ``RegexBuilder/Optionally``
|
|
- ``RegexBuilder/ZeroOrMore``
|
|
- ``RegexBuilder/OneOrMore``
|
|
- ``RegexBuilder/Repeat``
|
|
- ``RegexBuilder/Local``
|
|
|
|
### Captures
|
|
|
|
- ``RegexBuilder/Capture``
|
|
- ``RegexBuilder/TryCapture``
|
|
- ``RegexBuilder/Reference``
|
|
|
|
### Builders
|
|
|
|
- ``RegexBuilder/RegexComponentBuilder``
|
|
- ``RegexBuilder/AlternationBuilder``
|