Swift 5.7 RegexBuilder convert Array of strings to Regex - ChoiceOf options programatically

384 views Asked by At

I would love to take an array of strings

let array = ["one", "two", "three", "four"]

and convert it to the regex builder equivalent of:

Regex {
 ChoiceOf{
  "one"
  "two"
  "three"
  "four"
 }
}

or basically the equivelant of:

/one|two|three|four/

so far I have tried:

let joinedArray = array.joined(separator: "|")
let choicePattern = Regex(joinedArray)

I know that using Regex() throws and I need to handle that somehow but even when I do, I don't seem to get it to work.

Does anyone know how to do this?

0

There are 0 answers