Regex Builder Regex

Are there any differences between:

From apple xcode → refactor " Convert to Regex Builder"

let lowercaseLetters = Regex {
    ZeroOrMore(("a"..."z"))
}

From the book structure:

let RBlowercaseLetters = Regex {
    ZeroOrMore {
        "a" ... "z"
    }
}
let fixedWithBoundaries = Regex {
    ChoiceOf {
        Regex {
            Anchor.wordBoundary
            OneOrMore(("a"..."z"))
            ZeroOrMore(("0"..."9"))
            Anchor.wordBoundary
        }
        Regex {
            Anchor.wordBoundary
            ZeroOrMore(("a"..."z"))
            OneOrMore(("0"..."9"))
            Anchor.wordBoundary
        }
    }
}

Also that one add “Anchor.wordBoundary” for each regex

Hello @abil02 ,
for the first question, there is no difference between the two representations.

For the second question:

The original expression had the wordBoundary for each regex already /\b[a-z]+[0-9]*\b|\b[a-z]*[0-9]+\b/

I can build that expression without duplicating the \b for each half using grouping. but that would have made the expression look a little bit more complicated unnecessarily.
Apple’s refactor tool just converts things as they are.

With RegexBuilder, you can control the grouping a little easier by moving the wordBoundary outside of the ChoiceOf which what is expressed in the book and it actually mentions that:

This time, the wordBoundary is present outside of the or operator ChoiceOf . You can control the groupings that fall within the ChoiceOf block.

2 Likes

thank you for explanation.