let words = ["@apple", "#swift"]
let regex = Regex {
ChoiceOf {
ForEach(words, id: \.self) { word in
OneOrMore(word)
}
}
}
I want to use ForEach in RegexBuilder like SwiftUI
I use RegexComponentBuilder.buildPartialBlock
but different result
let words = ["@apple", "#swift"]
func getRegex() -> some RegexComponent {
var regex = RegexComponentBuilder.buildPartialBlock(first: Regex { words.first! } )
for word in words[1...] {
regex = RegexComponentBuilder.buildPartialBlock(accumulated: regex, next: Regex { word })
}
return ChoiceOf { regex }
}
let regex1 = getRegex()
let regex2 = Regex {
ChoiceOf {
"@swift"
"#swift"
}
}
let string = "aaa @swift #swift aaa"
for match in string.matches(of: regex1) {
print(String(string[match.range]))
} // print ""
for match in string.matches(of: regex2) {
print(String(string[match.range]))
} // @swift #swift