<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/RegexComponent",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:17_StringProcessing14RegexComponentP"
  },
  "title" : "RegexComponent"
}
-->

# RegexComponent

A type that represents a regular expression.

```
protocol RegexComponent<RegexOutput>
```

## Overview

You can use types that conform to `RegexComponent` as parameters to string
searching operations and inside `RegexBuilder` closures.

## Topics

### Creating a regex component

[`init(CharacterClass, CharacterClass...)`](/documentation/Swift/RegexComponent/init(_:_:))

Creates a character class that combines the given classes in a union.

### Getting a regex from a component

[`var regex: Regex<Self.RegexOutput>`](/documentation/Swift/RegexComponent/regex)

The regular expression represented by this component.

### Matching substring sequences

[`static func anyOf<S>(S) -> CharacterClass`](/documentation/Swift/RegexComponent/anyOf(_:)-3pexl)

Returns a character class that matches any Unicode scalar in the given
sequence.

[`static func anyOf<S>(S) -> CharacterClass`](/documentation/Swift/RegexComponent/anyOf(_:)-4xgea)

Returns a character class that matches any character in the given string
or sequence.

[`static var any: CharacterClass`](/documentation/Swift/RegexComponent/any)

A character class that matches any element.

[`static var anyGraphemeCluster: CharacterClass`](/documentation/Swift/RegexComponent/anyGraphemeCluster)

A character class that matches any single `Character`, or extended
grapheme cluster, regardless of the current semantic level.

[`static var anyNonNewline: CharacterClass`](/documentation/Swift/RegexComponent/anyNonNewline)

A character class that matches any element that isn’t a newline.

[`static var digit: CharacterClass`](/documentation/Swift/RegexComponent/digit)

A character class that matches any digit.

[`static var hexDigit: CharacterClass`](/documentation/Swift/RegexComponent/hexDigit)

A character class that matches any hexadecimal digit.

[`static var word: CharacterClass`](/documentation/Swift/RegexComponent/word)

A character class that matches any element that is a “word character”.

### Matching whitespace and line endings

[`static var horizontalWhitespace: CharacterClass`](/documentation/Swift/RegexComponent/horizontalWhitespace)

A character class that matches any element that is classified as
horizontal whitespace.

[`static var newlineSequence: CharacterClass`](/documentation/Swift/RegexComponent/newlineSequence)

A character class that matches any newline sequence.

[`static var verticalWhitespace: CharacterClass`](/documentation/Swift/RegexComponent/verticalWhitespace)

A character class that matches any element that is classified as
vertical whitespace.

[`static var whitespace: CharacterClass`](/documentation/Swift/RegexComponent/whitespace)

A character class that matches any element that is classified as
whitespace.

### Matching dates and times

[`static func date(Date.FormatStyle.DateStyle, locale: Locale, timeZone: TimeZone, calendar: Calendar?) -> Date.ParseStrategy`](/documentation/Swift/RegexComponent/date(_:locale:timeZone:calendar:))

Creates a regex component that matches a localized date string formatted in accordance with a style, capturing it as a Foundation date.

[`static func date(format: Date.FormatString, locale: Locale, timeZone: TimeZone, calendar: Calendar?, twoDigitStartDate: Date) -> Self`](/documentation/Swift/RegexComponent/date(format:locale:timeZone:calendar:twoDigitStartDate:))

Creates a regex component that matches a localized date string formatted in accordance with a format string, capturing it as a Foundation date.

[`static func dateTime(date: Date.FormatStyle.DateStyle, time: Date.FormatStyle.TimeStyle, locale: Locale, timeZone: TimeZone, calendar: Calendar?) -> Date.ParseStrategy`](/documentation/Swift/RegexComponent/dateTime(date:time:locale:timeZone:calendar:))

Creates a regex component that matches a localized date and time string, capturing it as a Foundation date.

[`static var iso8601: Date.ISO8601FormatStyle`](/documentation/Swift/RegexComponent/iso8601)

A regex component that matches a default ISO 8601-formatted date string, capturing it as a Foundation date.

[`static func iso8601Date(timeZone: TimeZone, dateSeparator: Date.ISO8601FormatStyle.DateSeparator) -> Self`](/documentation/Swift/RegexComponent/iso8601Date(timeZone:dateSeparator:))

Creates a regex component that matches an ISO 8601-formatted date string, capturing it as a Foundation date in the specified time zone.

[`static func iso8601(timeZone: TimeZone, includingFractionalSeconds: Bool, dateSeparator: Date.ISO8601FormatStyle.DateSeparator, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator, timeSeparator: Date.ISO8601FormatStyle.TimeSeparator) -> Self`](/documentation/Swift/RegexComponent/iso8601(timeZone:includingFractionalSeconds:dateSeparator:dateTimeSeparator:timeSeparator:))

Creates a regex component that matches an ISO 8601-formatted date string, capturing the matched substring as a Foundation date in the specified time zone.

[`static func iso8601WithTimeZone(includingFractionalSeconds: Bool, dateSeparator: Date.ISO8601FormatStyle.DateSeparator, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator, timeSeparator: Date.ISO8601FormatStyle.TimeSeparator, timeZoneSeparator: Date.ISO8601FormatStyle.TimeZoneSeparator) -> Self`](/documentation/Swift/RegexComponent/iso8601WithTimeZone(includingFractionalSeconds:dateSeparator:dateTimeSeparator:timeSeparator:timeZoneSeparator:))

Creates a regex component that matches an ISO 8601-formatted date string that includes a time zone component, capturing the matched substring as a Foundation date.

### Matching numeric formats

[`static func localizedInteger(locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedInteger(locale:))

Creates a regex component that matches a localized numeric string, capturing it as an integer value.

[`static func localizedDouble(locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedDouble(locale:))

Creates a regex component that matches a localized numeric string, capturing it as a double-precision floating-point value.

[`static func localizedDecimal(locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedDecimal(locale:))

Creates a regex component that matches a localized decimal string, capturing it as a Foundation decimal.

[`static func localizedCurrency(code: Locale.Currency, locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedCurrency(code:locale:))

Creates a regex component that matches a localized currency string, capturing it as a decimal value.

[`static func localizedIntegerCurrency(code: Locale.Currency, locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedIntegerCurrency(code:locale:))

Creates a regex component that matches a localized currency string, capturing it as an integer value.

[`static func localizedIntegerPercentage(locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedIntegerPercentage(locale:))

Creates a regex component that matches a localized percentage string, capturing it as a double-precision floating-point value.

[`static func localizedDoublePercentage(locale: Locale) -> Self`](/documentation/Swift/RegexComponent/localizedDoublePercentage(locale:))

Creates a regex component that matches a localized percentage string, capturing it as a double-precision floating-point value.

### Matching URLs

[`static func url(scheme: URL.ParseStrategy.ComponentParseStrategy<String>, user: URL.ParseStrategy.ComponentParseStrategy<String>, password: URL.ParseStrategy.ComponentParseStrategy<String>, host: URL.ParseStrategy.ComponentParseStrategy<String>, port: URL.ParseStrategy.ComponentParseStrategy<Int>, path: URL.ParseStrategy.ComponentParseStrategy<String>, query: URL.ParseStrategy.ComponentParseStrategy<String>, fragment: URL.ParseStrategy.ComponentParseStrategy<String>) -> Self`](/documentation/Swift/RegexComponent/url(scheme:user:password:host:port:path:query:fragment:))

Creates a regex component that matches a URL substring, capturing it as a Foundation URL.

### Supporting types

[`associatedtype RegexOutput`](/documentation/Swift/RegexComponent/RegexOutput)

The output type for this regular expression.

[`typealias DateStyle`](/documentation/Swift/RegexComponent/DateStyle)

A type alias to use when matching date components in a regular expression.

[`typealias TimeStyle`](/documentation/Swift/RegexComponent/TimeStyle)

A type alias to use when matching time components in a regular expression.

### Type Properties

[`static var http: Date.HTTPFormatStyle`](/documentation/Swift/RegexComponent/http)

Creates a regex component to match an HTTP date and time, such as “2015-11-14’T’15:05:03’Z’”, and capture the string as a `Date` using the time zone as specified in the string.

[`static var httpComponents: DateComponents.HTTPFormatStyle`](/documentation/Swift/RegexComponent/httpComponents)

Creates a regex component to match an HTTP date and time, such as “2015-11-14’T’15:05:03’Z’”, and capture the string as a `DateComponents` using the time zone as specified in the string.

[`static var iso8601Components: DateComponents.ISO8601FormatStyle`](/documentation/Swift/RegexComponent/iso8601Components)

Creates a regex component to match an ISO 8601 date and time, such as “2015-11-14’T’15:05:03’Z’”, and capture the string as a `DateComponents` using the time zone as specified in the string.

### Type Methods

[`static func iso8601Components(timeZone: TimeZone, includingFractionalSeconds: Bool, dateSeparator: Date.ISO8601FormatStyle.DateSeparator, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator, timeSeparator: Date.ISO8601FormatStyle.TimeSeparator) -> Self`](/documentation/Swift/RegexComponent/iso8601Components(timeZone:includingFractionalSeconds:dateSeparator:dateTimeSeparator:timeSeparator:))

Creates a regex component to match an ISO 8601 date and time string without time zone, and capture the string as a `DateComponents` using the specified `timeZone`. If the string contains time zone designators, matches up until the start of time zone designators.

[`static func iso8601ComponentsWithTimeZone(includingFractionalSeconds: Bool, dateSeparator: Date.ISO8601FormatStyle.DateSeparator, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator, timeSeparator: Date.ISO8601FormatStyle.TimeSeparator, timeZoneSeparator: Date.ISO8601FormatStyle.TimeZoneSeparator) -> Self`](/documentation/Swift/RegexComponent/iso8601ComponentsWithTimeZone(includingFractionalSeconds:dateSeparator:dateTimeSeparator:timeSeparator:timeZoneSeparator:))

Creates a regex component to match an ISO 8601 date and time string, including time zone, and capture the string as a `DateComponents` using the time zone as specified in the string.

[`static func iso8601DateComponents(timeZone: TimeZone, dateSeparator: Date.ISO8601FormatStyle.DateSeparator) -> Self`](/documentation/Swift/RegexComponent/iso8601DateComponents(timeZone:dateSeparator:))

Creates a regex component to match an ISO 8601 date string, such as “2015-11-14”, and capture the string as a `DateComponents`. The captured `DateComponents` would be at midnight in the specified `timeZone`.

## Relationships

### Conforming Types

[`Capture`](/documentation/RegexBuilder/Capture)

[`String`](/documentation/Swift/String)

[`CharacterClass`](/documentation/RegexBuilder/CharacterClass)

[`Regex`](/documentation/Swift/Regex)

[`Repeat`](/documentation/RegexBuilder/Repeat)

[`Scalar`](/documentation/Swift/Unicode/Scalar)

[`Substring`](/documentation/Swift/Substring)

[`ChoiceOf`](/documentation/RegexBuilder/ChoiceOf)

[`Lookahead`](/documentation/RegexBuilder/Lookahead)

[`Reference`](/documentation/RegexBuilder/Reference)

[`One`](/documentation/RegexBuilder/One)

[`TryCapture`](/documentation/RegexBuilder/TryCapture)

[`OneOrMore`](/documentation/RegexBuilder/OneOrMore)

[`Optionally`](/documentation/RegexBuilder/Optionally)

[`Anchor`](/documentation/RegexBuilder/Anchor)

[`Local`](/documentation/RegexBuilder/Local)

[`NegativeLookahead`](/documentation/RegexBuilder/NegativeLookahead)

[`Character`](/documentation/Swift/Character)

[`ZeroOrMore`](/documentation/RegexBuilder/ZeroOrMore)

### Inherited By

[`CustomConsumingRegexComponent`](/documentation/Swift/CustomConsumingRegexComponent)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)