Reputation: 1720
I need to use NSRegularExpression in my Swift application to check if latitude and longitude coordinates informed by user is in correct format.
My regular expression: -?[0-9]{1,3}[.]{1}[0-9]{6}
This regular expression work but not fully. It make to:
-
.
is necessaryThe problems are:
?.-33.476543
-33555.476543
.
the expression return true: -33.476543546565565765
It's normal but how can I create a "patern" to force user to informed only 6 numbers or only begin by -
character for exemple?
The regular expression in code:
let regex = NSRegularExpression("-?[0-9]{1,3}[.]{1}[0-9]{6}")
if regex.matches(self.latitude) && regex.matches(self.longitude) {
self.coordinatesValid = true
} else {
self.coordinatesValid = false
}
My NSRegularExpression extension:
import Foundation
extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illegal regular expression: \(pattern).")
}
}
func matches(_ string: String) -> Bool {
let range = NSRange(location: 0, length: string.utf16.count)
return firstMatch(in: string, options: [], range: range) != nil
}
}
Upvotes: 1
Views: 910
Reputation: 156
I use this string extension for RexExp:
extension String {
func match(_ regularExpression: String) -> Bool {
return self.range(of: regularExpression, options: .regularExpression) != nil
}
}
Swift 5 have added literal strings. This is perfect to use with regular expressions.
You could write regular expressions without scape the string like this:
#"([\w!#$%&'*+/=?`{|}~^-])+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}"#
Note that your string become with # that means that is a literal string and with this kind of string if you write for example \n that not mean a brake line.
Upvotes: 0
Reputation: 18591
You can try this:
"^-?[0-9]{1,3}[.]{1}[0-9]{6}$"
With:
^
: Start of a string$
: End of a stringHere are some test cases:
let latitudes = ["?.-33.476543",
"-33555.476543",
"-33.476543546565565765",
"abc-333.476543xyz",
"-333.476543",
]
let regex = NSRegularExpression("^-?[0-9]{1,3}[.]{1}[0-9]{6}$")
for latitude in latitudes {
print(regex.matches(latitude), "\t", latitude)
}
Here is the output:
false ?.-33.476543 false -33555.476543 false -33.476543546565565765 false abc-333.476543xyz true -333.476543
Upvotes: 1