calvin sugianto
calvin sugianto

Reputation: 640

Regex in Swift gives different result

I am using this regex in my Swift,

    let regex = try! NSRegularExpression(pattern: "^[a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+(?:[-' ][a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+)*$")
    let range = NSRange(location: 0, length: newValue.utf16.count)
    
    if regex.firstMatch(in: value, options: [], range: range) == nil {
        print("Not passed")
    } { 
        print("Passed") 
    }

but it gives different result from the website https://regex101.com/r/EXWsK1/2, what am i doing wrong here?

In my swift code, it should accept the aposthropes character such as Le'brahm, O'Niel, etc like the example on the website, but it does not.

Upvotes: 2

Views: 86

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626826

You need to add the curly single quotation marks, ‘’, to the pattern:

pattern: "^[a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+(?:[-'‘’ ][a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+)*$"

The straight quotation marks are replaced with curly ones by default.

Upvotes: 1

Related Questions