Regex Builder error message???

I'm debugging some Regex Builder code in my Playground. I run the following piece code:

let timeMatchWithout = possibleTime.firstMatch(of: timeWithoutSec)

and I get this error message:

Regex.Match optional storedCapture contains no some

What could this possibly mean? contains no some???

Here is a more complete snippet, if this helps:

let hourRef = Reference<Substring>()
let minuteRef = Reference<Substring>()
let hourReg = Regex {
  
        ChoiceOf {
            Capture(as: hourRef) {
                    One(.digit)
                    One(.digit)
            }
            Capture(as: hourRef) {
                    One(.digit)
            }
        }
        
    }
    
    let minuteReg = Regex {
        ChoiceOf {
            Capture(as: minuteRef) {
                One(.digit)
                One(.digit)
            }
            Capture(as: minuteRef) {
                One(.digit)
            }
        }
    }
let ampmRef = Reference<Substring>()
    let ampmReg = Regex {
        Capture(as: ampmRef) {
            ZeroOrMore {
                ChoiceOf {
                    One("am")
                    One("pm")
                    One("a.m.")
                    One("p.m.")
                }
            }
        } /* transform: {
            $0.lowercase
            
        } */
    }.ignoresCase()
let timeWithoutSec = Regex {
        hourReg
        One(":")
        minuteReg
        ZeroOrMore(.whitespace)
        ampmReg
    }.ignoresCase()
let possibleTime = "10:20 AM"
let timeMatchWithout = possibleTime.firstMatch(of: timeWithoutSec)

The last line produces the error message. Thanks for the help.

Note the removed transform: on the ampmReg definition. If that is included the compiler times out as noted in my previous post, yesterday.

I got this working by using the Repeat component instead of Capture in hourReg and minuteReg. I'm not sure why that should matter, but Repeat is cleaner. I also got the transform to work by returning a Substring in the transform: of the ampmReg Regex.

import Cocoa
import Foundation
import RegexBuilder

let hourRef = Reference<Substring>()
let minuteRef = Reference<Substring>()
let hourReg = Regex {
    Capture(as: hourRef) {
        ChoiceOf {
            Repeat(.digit, count: 2)
            Repeat(.digit, count: 1)
        }
    }
}

let minuteReg = Regex {
    Capture(as: minuteRef) {
        ChoiceOf {
            Repeat(.digit, count: 2)
            Repeat(.digit, count: 1)
        }
    }
        
}
let ampmRef = Reference<Substring>()
let ampmReg = Regex {
    Capture(as: ampmRef) {
        ChoiceOf {
            One("am")
            One("pm")
            One("a.m.")
            One("p.m.")
        }
    } transform: {
        return Substring($0.lowercased())
    }
}.ignoresCase()

let timeWithoutSec = Regex {
        hourReg
        One(":")
        minuteReg
        ZeroOrMore(.whitespace)
        ampmReg
    }.ignoresCase()
let possibleTime = "1:20pM"
let timeMatchWithout = possibleTime.firstMatch(of: timeWithoutSec)

However, this version requires an 'am', 'pm' , 'a.m.', or 'p.m.' to be part of the string. To make this optional I tried to change the timeWithoutSec Regex to:


let timeWithoutSec = Regex {
    hourReg
    One(":")
    minuteReg
    ZeroOrMore(.whitespace)
    ZeroOrMore {
        ampmReg
    }
}.ignoresCase()

But this results in that odd Regex.Match optional storedCapture contains no some error message. How can I make the ampmReg optional?

I tried reproducing your problem and couldn’t. Specifically:

  1. In Xcode 15.4 I chose File > New > Playground.

  2. I created a macOS > Blank playground.

  3. I entered the code from your second post.

  4. And replaced the timeWithoutSec regex with your updated snippet.

  5. The compiled just fine.

  6. When I ran it, I see ["1:20pM", "1", "20", "pM"] on the right for the last line.

What version of Xcode are you testing this on? Or is this perhaps the Swift Playgrounds app?

If you put your code into a small command-line tool project (in Xcode, choose File > New Project and then select macOS > Command Line Tool) does the problem reproduce there?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Regex Builder error message???
 
 
Q