<!--
{
  "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/url(scheme:user:password:host:port:path:query:fragment:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:17_StringProcessing14RegexComponentP10FoundationAD3URLV13ParseStrategyVRszrlE3url6scheme4user8password4host4port4path5query8fragmentA2H0dgH0Oy__SSG_A3tSy__SiGA3TtFZ"
  },
  "title" : "url(scheme:user:password:host:port:path:query:fragment:)"
}
-->

# url(scheme:user:password:host:port:path:query:fragment:)

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

```
static func url(scheme: URL.ParseStrategy.ComponentParseStrategy<String> = .required, user: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, password: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, host: URL.ParseStrategy.ComponentParseStrategy<String> = .required, port: URL.ParseStrategy.ComponentParseStrategy<Int> = .optional, path: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, query: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, fragment: URL.ParseStrategy.ComponentParseStrategy<String> = .optional) -> Self
```

## Parameters

`scheme`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the URL scheme component.

`user`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the user component.

`password`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the password component.

`host`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the host component.

`port`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the port component.

`path`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the path component.

`query`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the query component.

`fragment`

A <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> for matching the fragment component.

## Return Value

A `RegexComponent` that matches a URL.

## Discussion

All the parameters to this method take a <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy> value to configure the matching behavior for one component of the URL. The three possible values are:

- <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy/required> — The URL component needs to be present for matching to succeed.
- <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy/optional> — The URL component doesn’t need to be present for matching to succeed.
- <doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy/defaultValue(_:)> — If the URL component is absent, the captured URL contains the provided default value for the component.

The following example creates a [`Regex`](/documentation/Swift/Regex) that matches a URL, when it contains a scheme and a host. It then matches against a source string that contains a date formatted in the `en_US` locale, some whitespace, and a valid URL. The regex defines a default value for the port with
<doc://com.apple.documentation/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy/defaultValue(_:)>, and because the source URL doesn’t include a port, the captured URL adds it.

```
let source = "7/31/2022, 5:15:12 AM  https://www.example.com/productList?query=slushie"
let matcher = Regex {
    One(.dateTime(date: .numeric,
                  time: .standard,
                  locale: Locale(identifier: "en_US"),
                  timeZone: TimeZone(identifier: "PST")!))
    OneOrMore(.horizontalWhitespace)
    Capture {
        One(.url(scheme: .required,
                 user: .optional,
                 password: .optional,
                 host: .required,
                 port: .defaultValue(8088),
                 path: .optional,
                 query: .optional,
                 fragment: .optional))
    }
}
guard let match = source.firstMatch(of: matcher) else { return }
let url = match.1 // url = https://www.example.com:8088/productList?query=slushie
```

---

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)