---
title: URL.ParseStrategy
framework: foundation
role: symbol
role_heading: Structure
path: foundation/url/parsestrategy
---

# URL.ParseStrategy

A parse strategy for creating URLs from formatted strings.

## Declaration

```swift
struct ParseStrategy
```

## Overview

Overview Create an explicit URL.ParseStrategy to parse multiple strings according to the same parse strategy. The following example creates a customized strategy, then applies it to multiple URL candidate strings. let strategy = URL.ParseStrategy(     scheme: .defaultValue("https"),     user: .optional,     password: .optional,     host: .required,     port: .optional,     path: .required,     query: .required,     fragment: .optional) let urlStrings = [     "example.com?key1=value1", // no scheme or path     "https://example.com?key2=value2", // no path     "https://example.com", // no query     "https://example.com/path?key4=value4", // complete     "//example.com/path?key5=value5" // complete except for default-able scheme ] let urls = urlStrings.map { try? strategy.parse($0) } // [nil, nil, nil, Optional(https://example.com/path?key4=value4), Optional(https://example.com/path?key5=value5)] You don’t need to instantiate a parse strategy instance to parse a single string. Instead, use the URL initializer init(_:strategy:), passing in a string to parse and a customized strategy, typically created with one of the static accessors. The following example parses a URL string, with a custom strategy that provides a default value for the port component if the source string doesn’t specify one. let urlString = "https://internal.example.com/path/to/endpoint?key=value" let url = try? URL(urlString, strategy: .url     .port(.defaultValue(8080))) // https://internal.example.com:8080/path/to/endpoint?key=value

## Topics

### Creating a URL parse strategy

- [init(scheme:user:password:host:port:path:query:fragment:)](foundation/url/parsestrategy/init(scheme:user:password:host:port:path:query:fragment:).md)
- [URL.ParseStrategy.ComponentParseStrategy](foundation/url/parsestrategy/componentparsestrategy.md)

### Customizing strategy behavior

- [scheme(_:)](foundation/url/parsestrategy/scheme(_:).md)
- [user(_:)](foundation/url/parsestrategy/user(_:).md)
- [password(_:)](foundation/url/parsestrategy/password(_:).md)
- [host(_:)](foundation/url/parsestrategy/host(_:).md)
- [port(_:)](foundation/url/parsestrategy/port(_:).md)
- [path(_:)](foundation/url/parsestrategy/path(_:).md)
- [query(_:)](foundation/url/parsestrategy/query(_:).md)
- [fragment(_:)](foundation/url/parsestrategy/fragment(_:).md)
- [URL.ParseStrategy.ComponentParseStrategy](foundation/url/parsestrategy/componentparsestrategy.md)

### Parsing strings

- [parse(_:)](foundation/url/parsestrategy/parse(_:).md)

### Locating URLs with regular expressions

- [consuming(_:startingAt:in:)](foundation/url/parsestrategy/consuming(_:startingat:in:).md)

### Supporting Types

- [URL.ParseStrategy.RegexOutput](foundation/url/parsestrategy/regexoutput.md)

### Default Implementations

- [CustomConsumingRegexComponent Implementations](foundation/url/parsestrategy/customconsumingregexcomponent-implementations.md)
- [ParseStrategy Implementations](foundation/url/parsestrategy/parsestrategy-implementations.md)
- [RegexComponent Implementations](foundation/url/parsestrategy/regexcomponent-implementations.md)

## Relationships

### Conforms To

- [Copyable](swift/copyable.md)
- [CustomConsumingRegexComponent](swift/customconsumingregexcomponent.md)
- [Decodable](swift/decodable.md)
- [Encodable](swift/encodable.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [ParseStrategy](foundation/parsestrategy.md)
- [RegexComponent](swift/regexcomponent.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Parsing URLs

- [parseStrategy](foundation/url/formatstyle/parsestrategy.md)
