---
title: Regex
framework: swift
role: symbol
role_heading: Structure
path: swift/regex
---

# Regex

A regular expression.

## Declaration

```swift
struct Regex<Output>
```

## Overview

Overview Regular expressions are a concise way of describing a pattern, which can help you match or extract portions of a string. You can create a Regex instance using regular expression syntax, either in a regex literal or a string. // 'keyAndValue' is created using a regex literal let keyAndValue = /(.+?): (.+)/ // 'simpleDigits' is created from a pattern in a string let simpleDigits = try Regex("[0-9]+") You can use a Regex to search for a pattern in a string or substring. Call contains(_:) to check for the presence of a pattern, or firstMatch(of:) or matches(of:) to find matches. let setting = "color: 161 103 230" if setting.contains(simpleDigits) {     print("'\(setting)' contains some digits.") } // Prints "'color: 161 103 230' contains some digits." When you find a match, the resulting Regex.Match type includes an output property that contains the matched substring along with any captures: if let match = setting.firstMatch(of: keyAndValue) {     print("Key: \(match.1)")     print("Value: \(match.2)") } // Key: color // Value: 161 103 230 When you import the RegexBuilder module, you can also create Regex instances using a clear and flexible declarative syntax. Using this style, you can combine, capture, and transform regexes, RegexBuilder types, and custom parsers. note: Prior to Swift 6, you might need to write #/myregex/# instead of /myregex/ when you make a regular expression using a literal. For more information, see Regular Expression Literals in  The Swift Programming Language.

## Topics

### Structures

- [Regex.Match](swift/regex/match.md)

### Initializers

- [init(_:)](swift/regex/init(_:)-4ef55.md)
- [init(_:)](swift/regex/init(_:)-52kg.md)
- [init(_:)](swift/regex/init(_:)-92siq.md)
- [init(_:as:)](swift/regex/init(_:as:)-2ucu7.md)
- [init(_:as:)](swift/regex/init(_:as:)-5z5nu.md)
- [init(verbatim:)](swift/regex/init(verbatim:).md)

### Instance Properties

- [regex](swift/regex/regex.md)

### Instance Methods

- [anchorsMatchLineEndings(_:)](swift/regex/anchorsmatchlineendings(_:).md)
- [asciiOnlyCharacterClasses(_:)](swift/regex/asciionlycharacterclasses(_:).md)
- [asciiOnlyDigits(_:)](swift/regex/asciionlydigits(_:).md)
- [asciiOnlyWhitespace(_:)](swift/regex/asciionlywhitespace(_:).md)
- [asciiOnlyWordCharacters(_:)](swift/regex/asciionlywordcharacters(_:).md)
- [contains(captureNamed:)](swift/regex/contains(capturenamed:).md)
- [dotMatchesNewlines(_:)](swift/regex/dotmatchesnewlines(_:).md)
- [firstMatch(in:)](swift/regex/firstmatch(in:)-45hz7.md)
- [firstMatch(in:)](swift/regex/firstmatch(in:)-6s8x0.md)
- [ignoresCase(_:)](swift/regex/ignorescase(_:).md)
- [matchingSemantics(_:)](swift/regex/matchingsemantics(_:).md)
- [prefixMatch(in:)](swift/regex/prefixmatch(in:)-1an24.md)
- [prefixMatch(in:)](swift/regex/prefixmatch(in:)-5oh8i.md)
- [repetitionBehavior(_:)](swift/regex/repetitionbehavior(_:).md)
- [wholeMatch(in:)](swift/regex/wholematch(in:)-8hr88.md)
- [wholeMatch(in:)](swift/regex/wholematch(in:)-9do8t.md)
- [wordBoundaryKind(_:)](swift/regex/wordboundarykind(_:).md)

### Type Aliases

- [Regex.RegexOutput](swift/regex/regexoutput.md)

## Relationships

### Conforms To

- [RegexComponent](swift/regexcomponent.md)

## See Also

### Regular Expressions

- [RegexRepetitionBehavior](swift/regexrepetitionbehavior.md)
- [RegexSemanticLevel](swift/regexsemanticlevel.md)
- [RegexWordBoundaryKind](swift/regexwordboundarykind.md)
- [AnyRegexOutput](swift/anyregexoutput.md)
- [RegexComponent](swift/regexcomponent.md)
- [CustomConsumingRegexComponent](swift/customconsumingregexcomponent.md)
