2

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?

The Fox
  • 1,189
  • 1
  • 6
  • 10
  • This is basically the same question as [this](https://stackoverflow.com/q/73915873/5133585). See my answer there - it's kind of a hack IMO. `Regex(joinedArray)` should have worked as well though. Why and how didn't it work exactly? – Sweeper Feb 17 '23 at 01:49
  • @Sweeper Thank you, I actually wasn't too far off, I just needed to not be so tired and frustrated, your linked answer was very helpful. By chance do you happen to know anything about the ".mappedOutput" mentioned in the evolution article you mentioned? It does not seem to be available in Xcode. – The Fox Feb 17 '23 at 17:16
  • Huh, I didn't notice that they also mentioned that in the SE proposal, because I had [a similar idea](https://stackoverflow.com/q/73815622/5133585). I guess they didn't implement it in the end. – Sweeper Feb 18 '23 at 01:31

0 Answers0