Regex Sample Code - Errors with code at [3:49}

Version 14.0 beta (14A5228q) I'm getting errors when using the sample code at [3:49], in a playground. The code is below. The line with the .split throws an error, as does the switch statement. If the parameter named "separator" is replaced with "by", then the line works. Unable to fine a way to get the switch to work.

let input = "name: John Appleseed, user_id: 100" let regex = /user_id:\s*(\d+)/ input.firstMatch(of: regex) // Regex.Match<(Substring, Substring)> input.wholeMatch(of: regex) // nil input.prefixMatch(of: regex) // nil input.starts(with: regex) // false input.replacing(regex, with: "456") // "name: John Appleseed, 456" input.trimmingPrefix(regex) // "name: John Appleseed, user_id: 100" input.split(separator: /\s*,\s*/) // ["name: John Appleseed", "user_id: 100"]

switch "abc" { case /\w+/: print("It's a word!") }

Replies

I concur that .split(by: RegexComponent) is the way to go as it accepts RegexComponent rather than .split(separator: Character) which just accepts a single character.

Here is an example of a case statement using Regexs.

//  does a string start with a word of one or more letters?
var possibleWords   = ["word", " text", "1stop", "-spaced", "987-", "987again"]

for word in possibleWords {
    switch true {
    case word.starts(with: /\d{3}-/) :
        print("'\(word)' starts with  3-digits and a hyphen.")
    case word.starts(with: /\d+/) :
        print("'\(word)' starts with digit(s).")
    case word.starts(with: /\w+/) :
        print("'\(word)' starts with a word.")
    case word.starts(with: /\s+/) :
        print("'\(word)' starts with a whitespace.")
    default:
        print("'\(word)' had no Matches")
    }
}

The output from this is

'word' starts with a word.
' text' starts with a whitespace.
'1stop' starts with digit(s).
'-spaced' had no Matches
'987-' starts with  3-digits and a hyphen.
'987again' starts with digit(s).

Hope this helps

Chris