Bernd
Bernd

Reputation: 11493

Matching variations of a String using a single RegEx

Is it possible to check for multiple variations of a string using a single Regular Expressions?

I want find matches of the following two string variations in a longer string.

E.g.

Currently my regex pattern looks like the following, working only for one specific variation

    var pattern = "yearly"
    var error: NSError?
    var regularExpression = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)!
    let matchingItems = regularExpression.matchesInString(entryString, options: nil, range:NSMakeRange(0, countElements(entryString)))

Can the matching of both cases ("yearly" + "every year") be combined using one Regular Expression or do I need two separate Regular Expressions for this?

Upvotes: 1

Views: 1569

Answers (1)

vks
vks

Reputation: 67968

var pattern = "yearly|(?:every year)"

You can try this way using | or operator.

Upvotes: 2

Related Questions