Search code examples
iosswiftxcodeswift-regexbuilder

Swift RegexBuilder new syntax 2 or more spaces


Hi I'm trying to build a regular expression builder to detect 2 or more spaces or a tab, so (let twoOrMoreSpacesOrTab = /\s{2,}|\t/)

How to build this using a Regex Builder?

I tried this but its not 100% acurate:

ChoiceOf {
    OneOrMore("  ")
    One("\t")
}

The problem here is that is trying to match multiples of 2 white spaces, and I want to consume the whole thing.


Solution

  • This might work.

    let twoOrMoreSpacesOrTab = Regex {
        ChoiceOf {
            Repeat(2...) {
                One(.whitespace)
            }
            One("\t")
        }
    }