slashlos
slashlos

Reputation: 933

User Agent Parsing in swift

Cobbling together bits I've come up with this but regex does not like pattern: from SO

struct UAHelpers {
    static func isValid(uaString: String) -> Bool {
        let regex = try! NSRegularExpression(pattern: ".+?[\/\s][\d.]+")
        return (regex.firstMatch(in: uaString, range: uaString.nsrange) != nil)
    }
}

Invalid escape sequence in literal

which looks ok to me - but no regex expert yet regex.com appears to like it?
Also tried pattern: \(([^(]*)\) - no joy.

I'm trying to allow user (at own peril) to enter yet provide some parsing.

Upvotes: 0

Views: 314

Answers (1)

OOPer
OOPer

Reputation: 47896

To represent regex pattern .+?[/\s][\d.]+ as a Swift String, backslashes \ needs to be escaped.

(In Swift String, / has no need to be escaped.)

Try using ".+?[/\\s][\\d.]+".

Though, I'm not sure if this pattern really extracts what you expect.

Upvotes: 1

Related Questions