Regex expression in .split generates cannot convert type

Using Version 14.0 beta (14A5228q) on Monterey 12.4

I am trying to follow the 'Meet Swift regex' presentation dated 7 June but I have stumbled on the most basic of errors that I cannot get around.

At 3:56 in the video, the following line of code is shown

let transaction = "DEBIT     03/05/2022    Doug's Dugout Dogs         $33.27"

let fragments = transaction.split(separator: /\s{2,}|\t/)
// ["DEBIT", "03/05/2022", "Doug's Dugout Dogs", "$33.27"]``

The '/' of the separator string generates the error:

  • error: cannot convert value of type 'Regex' to expected argument type 'String.Element' (aka 'Character')

I am embarrassed but also stuck on the simple issue and I cannot figure out what I am missing.

TIA

Chris

Replies

To fix this specific problem, add a space between the trailing / and the the trailing ):

let fragments = transaction.split(by: /\s{2,}|\t/ )

Fitting the regex / literal syntax into Swift without breaking existing code requires quite a bit of parser gymnastics. I’m not fully up to speed on this issue but I believe that the current Xcode beta does not contain all the necessary tweaks to the parser, and hence the need for this whitespace. If you’re curious about the details, hop on over to Swift Evolution where this stuff has been discussed in laborious detail. Indeed, as of this morning discussions were still ongoing in this thread.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • The parser has no problems recognizing the Regex compact notation in a playground that I created to reproduce the problem. I declare the Regex on a separate line to make sure.

    let transaction = "DEBIT     03/05/2022    Doug's Dugout Dogs         $33.27" let r = /\s{2,}|\t/ let fragments = transaction.split(separator: r)

    The build error is related to line 3. String seems to be missing a variant of split(separator:) that accepts a Regex. Missing in the doc pages as well.

Add a Comment

I can reproduce the problem in a playground in Xcode 14A5228q on macOS 13 (22A5266r).

I moved the Regex declaration to a separate line to rule out a parsing problem in the compiler front-end:

let transaction = "DEBIT     03/05/2022    Doug's Dugout Dogs         $33.27"
let r = /\s{2,}|\t/
let fragments = transaction.split(separator: r)

The error message Cannot convert value of type 'Regex<Substring>' to expected argument type 'String.Element' (aka 'Character') is shown for line 3.

Looks like String is missing a variant of split(separator:...) that accepts a Regex. There is no such function in the doc pages either.

Another problem I'm having with Regex in Xcode 14A5228q, is that the parsing of the compact notation fails in my Swift package project. That is, if I copy the above three lines from the playground into my project, there will be an error in line 2 because the compiler fails to recognize / as a Regex start token.

Add a Comment