# Applying Regex to Substring

**URL:** <https://forums.swift.org/t/applying-regex-to-substring/62004>\
**Category:** Pitches\
**Tags:** regex\
**Created:** [December 12, 2022, 11:23pm UTC](https://forums.swift.org/t/applying-regex-to-substring/62004 "2022-12-12T23:23:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjhomer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bjhomer/32/313_2.png) [@bjhomer](https://forums.swift.org/u/bjhomer)\
**Post date:** [December 12, 2022, 11:23pm UTC](https://forums.swift.org/t/applying-regex-to-substring/62004/1 "2022-12-12T23:23:56Z")

</div>

Many methods on String return `Substring`. I found myself wanting to use a `Regex` to parse part of one of these substrings recently, but found that `Regex` only works on a full String.

Is there a reason that Regex could not work with Substrings as well?

---

<div class="post-metadata">

**Author:** ![Jeehut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeehut/32/21743_2.png) [@Jeehut](https://forums.swift.org/u/Jeehut)\
**Post date:** [January 10, 2023, 12:46pm UTC](https://forums.swift.org/t/applying-regex-to-substring/62004/2 "2023-01-10T12:46:37Z")

</div>

@bjhomer What APIs are you exactly missing? I've just tested and the ones I personally would expect seem to work with `Substring`, too. Running this code in a playground worked as expected:

```swift
import Foundation

let string: String = "Hello, world!"
let substring: Substring = string.split(separator: ", ").first!

let regex = /abc/
string.matches(of: regex) // => []
substring.matches(of: regex) // => []

```

Can you be more specific? To me it seems like `Substring` is already supported ...

---

<div class="post-metadata">

**Author:** ![bjhomer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bjhomer/32/313_2.png) [@bjhomer](https://forums.swift.org/u/bjhomer)\
**Post date:** [January 10, 2023, 9:24pm UTC](https://forums.swift.org/t/applying-regex-to-substring/62004/3 "2023-01-10T21:24:28Z")

</div>

Ah, interesting. What I'm actually seeing is that although regexes support both `String` and `Substring` arguments, they don't support `StringProtocol`. I have a bunch of functions like this:

```swift
func parseLine(_ line: some StringProtocol) {
  let regex = /You earned (\d+) points by collecting (\d+) gems/
  if let match = try regex.wholeMatch(in: line) {
    // use the match here
  }
}

```

This makes it easy for me to pass it a `Substring` that arises as part of parsing a file line-by-line, but I can also test it individually by passing an individual string. But I get the error:

```swift
// error: No exact matches in call to instance method 'wholeMatch'

```

… because `wholeMatch` doesn't take a `some StringProtocol`. It only has concrete overrides for String and Substring.

I wonder if it would be possible to define the regex operations in terms of `StringProtocol`

---

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [January 13, 2023, 10:15pm UTC](https://forums.swift.org/t/applying-regex-to-substring/62004/4 "2023-01-13T22:15:43Z")

</div>

> [@bjhomer](#):
>
> I wonder if it would be possible to define the regex operations in terms of `StringProtocol`

This idea was considered and dismissed by one of the authors of SE-0357 in its review thread:

> [@SE-0357: Regex String Processing Algorithms](https://forums.swift.org/t/se-0357-regex-string-processing-algorithms/57225/6):
>
> > [@SE-0357: Regex String Processing Algorithms](https://forums.swift.org/t/se-0357-regex-string-processing-algorithms/57225/4):
> >
> > One more thing.
> > 
> > There is explanation why `Collection` is used instead of `Sequence` and why it's `Collection where SubSequence == Substring` . But, is there any explanation why not `StringProtocol where SubSequence == Substring` and `StringProtocol where Self: RangeReplaceableCollection` ?
> > 
> > This is just a question. I don’t necessarily hope that, though.
> 
> `StringProtocol` is.... not well designed and insufficient for this purpose alone. We'd still need the `where` as you pointed out. At that point, overly constraining ourselves isn't that useful and we definitely don't want to be pushing anyone to conform to `StringProtocol`.
> 
> Being able to vend a `Substring` means that you're also creating a proper `String` for the `base` property, and doing so as a subsequence means you're not (likely) doing a copy every time. It's conceivable that we'll support some notion of (safely!) shared strings in the future with move semantics and it's conceivable that a type may vend a string-view. At that point, it's just better to use the slice (or string) as the adapter type without having another conformance to `StringProtocol`.
> 
> TL;DR: `StringProtocol` doesn't get us anything and we want there to be less of it in the future, not more.

Instead of using `StringProtocol`, why not just use `Substring`? You can easily create a `Substring` from a `String` like so:

```swift
let substring = "string"[...]

```
