Search code examples
swiftregexstring

Capturing variations in string in Swift Regex


I would like to be able to capture two possibilities for the way something can be written. As I work through a large string with multiple entries, I am noticing that some entries are written differently but mean the same thing.

What I would like is to be able to use the same variable but capture variations on the same word.

I am aware that this example below is incorrect, but was wondering if there was a way to do something along the lines of this?

struct NotamRegexPatterns {

static let holdingPoint: Regex = Regex {
        Capture {
            "HOLDING POINT" || "HLDG POINT"
        }
    }

}

Solution

  • You can use ChoiceOf:

    Capture {
        ChoiceOf {
            "HOLDING POINT"
            "HLDG POINT"
        }
    }
    

    Or you can always just fallback to regex literal syntax:

    Capture {
        /HOLDING POINT|HLDG POINT/
    }