How to validate a name(John D'Largy) in iOS Swift4.2? - ios

i tried all possibilities in stack-overflow (link1, link2) answers no use for me.
I am using following Regex to validate a first name. In online case (OnlineRegex) it is working fine but when i implemented in mobile it is not working.
Please help me
func isValidName() -> Bool {
let RegEx = "^[a-zA-Z]+(([\\'\\,\\.\\-\\ ][a-zA-Z ])?[a-zA-Z]*)*$"
let Test = NSPredicate(format:"SELF MATCHES %#", RegEx)
return Test.evaluate(with: self)
}
i am calling above function as
let str = "John D'Largy"
if str.isValidName(){
print("Valid")
}else{ print("Not valid")}
Output : "Valid"
Same function i am calling to validate my first text feild i am getting "Not valid"
if firstNameTxt.text.isValidName(){
print("Valid")
}else{ print("Not valid")}
i entered same text in mobile keyword
OutPut: "Not valid"
Did i missing something? or Should i have to change regex value?.
Any suggestions.
Thanks in Advance.

You may use
(?:[a-zA-Z]+(?:['‘’,.\\s-]?[a-zA-Z]+)*)?
The code you have already requires the full string match and you need no explicit anchors like ^ / \A and $ / \z.
Also, since the single quotation marks are automatically converted to curly quotes, you should either add them to the regex or turn off the behavior.
One of the most important things about thi regex is that it should be able to match partially correct string, thus all of the parts are optional (i.e. they can match 0 chars). It is wrapped with an optional non-capturing group ((?:...)?) that matches 1 or 0 occurrences.
Regex details
[a-zA-Z]+ - 1 or more letters
(?: - start of the inner non-capturing group:
['‘’,.\\s-]? - 1 or 0 whitespaces, single quotes, hyphens
[a-zA-Z]+ - 1+ letters
)* - 0 or more repetitions.
Note: to match any Unbicode letter, use \\p{L} instead of [a-zA-Z].
Graph:

See, I tried your code in the playground and changed the syntax a bit but not the logic
Below is the code snippet, try running that in your playground
func isValidName(str: String) -> Bool {
let RegEx = "^[a-zA-Z]+(([\\'\\,\\.\\-\\ ][a-zA-Z ])?[a-zA-Z]*)*$"
let Test = NSPredicate(format:"SELF MATCHES %#", RegEx)
return Test.evaluate(with: str)
}
func check(){
let str = "John D'Largy"
if isValidName(str: str){
print("Valid")
}else{
print("Not valid")
}
}
check()
Hope it helps!

Related

Swift Regex to check if String is Amount format (2x, 3x,...)

I would like to check if a given String is in a amount format. A few examples for a better understanding:
Should return true:
2x
31x
3x abcd
Should return false:
2x3
3xabc
asdf3x
So in general: it has to start with a number (0-9), can be more digits. Right after the number a "x"/"X" should follow and after the "x"/"X" should be the end or a white space.
I am struggling to get this done.. This is what I tried:
func isAmountFormat() -> Bool {
return self.matches("[0-9]+[X,x]")
}
Can anyone help me out here?
You may use
"(?i)\\b[1-9][0-9]*x\\b"
See the regex demo. Details:
(?i) - case insensitive flag (in case you cannot pass the .caseInsensitive in your current regex matching method)
\b - a word boundary
[1-9] - a non-zero digit
[0-9]* - any zero or more digits
x - an X or x
\b - a word boundary.
See a Swift snippet:
let test = "3x abcd"
if test.range(of: "\\b[1-9][0-9]*x\\b", options: [.regularExpression, .caseInsensitive]) != nil {
print("Matched")
} else {
print("No match")
}
Note that if the match should occur at the start of the string, you may compile the regex object with the .anchored option, or replace the first \\b with ^.

Understanding function with string validation

Consider following:
extension String {
func isValidEmail() -> Bool {
let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
print(characterset)
if self.rangeOfCharacter(from: characterset.inverted) != nil {
return false
} else {
return true
}
}
}
var name = "Login"
name.isValidEmail() // print true
var incorretLogin = "Loginъ"
incorretLogin.isValidEmail() // print false
Yes, function is work. But im in confussion - how its work?
If i understand correct it work like that:
it take set of characters, then check if all of tested string characters contain symbols from set, and if it is not, then it return false.
Ok, but what is inverted for? If i remove inverted, result will be wrong:
var name = "Login"
name.isValidEmail() // false
var incorretLogin = "Logъin"
incorretLogin.isValidEmail() // false
Now i understand nothing.
If function check simply if string letters are from character set, then why is it matter if set inverted or not?
Could someone explain?
I play a bit in playground:
let characterset = CharacterSet(charactersIn: "a")
print(characterset)
print(characterset.inverted)
Print same result:
<CFCharacterSet Items(U+0061)>
<CFCharacterSet Items(U+0061)>
inverted "returns an inverted copy of the receiver." (see https://developer.apple.com/documentation/foundation/characterset).
In your case inverted means all the characters except the ones you provide in the initializer (all characters except letters and digits). So the method returns false if the email string contains any character that is not a letter or a digit.
Playground example:
According to the documentation
rangeOfCharacter(from:)
Finds and returns the range in the receiver of the first character from a given character set.
The receiver is the string being checked. When no character from the set is found in the string, nil is returned.
When the set is inverted, it contains all invalid characters. Hence, rangeOfCharacter(from:) returns the location of the first invalid character. That is why your first approach works.
When you remove inverted, the call returns the location of the first valid character. Since "Logъin" has both valid and invalid characters, both calls return false. If you call your second function on a string consisting entirely of invalid characters, e.g. "Логин", you would get true.
Note that you can simplify the implementation by removing if:
let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
return self.rangeOfCharacter(from: characterset.inverted) == nil

How to avoid an error due to special characters in search string when using Regex for a simple search in Swift?

I'm using Regex to search for a word in a textView. I implemented a textField and two switch as options (Whole words and Match case). All work fine when you enter a plain word in the search filed but I get an error when I enter a special character like \ or *.
The error I get is like this one:
Error Domain=NSCocoaErrorDomain Code=2048 "The value “*” is invalid." UserInfo={NSInvalidValue=*}
Is there a way to avoid this problem and have the code handle all the text like plain text?
Because I would like to search also for special characters, I would like to prefer to not interdict to enter them. At the beginning I though to programmatically add an escape backslash to all special character before to perform a search, but maybe there are some more smart approaches?
Here is the code I'm using (based on this tutorial: NSRegularExpression Tutorial: Getting Started)
struct SearchOptions {
let searchString: String
var replacementString: String
let matchCase: Bool
let wholeWords: Bool
}
extension NSRegularExpression {
convenience init?(options: SearchOptions) {
let searchString = options.searchString
let isCaseSensitive = options.matchCase
let isWholeWords = options.wholeWords
// handle case sensitive option
var regexOption: NSRegularExpressionOptions = .CaseInsensitive
if isCaseSensitive { // if it is match case remove case sensitive option
regexOption = []
}
// put the search string in the pattern
var pattern = searchString
// if it's whole word put the string between word boundary \b
if isWholeWords {
pattern = "\\b\(searchString)\\b" // the second \ is used as escape
}
do {
try self.init(pattern: pattern, options: regexOption)
} catch {
print(error)
}
}
}
You may use NSRegularExpression.escapedPatternForString:
Returns a string by adding backslash escapes as necessary to protect any characters that would match as pattern metacharacters.
Thus, you need
var pattern = NSRegularExpression.escapedPatternForString(searchString)
Also, note that this piece:
if isWholeWords {
pattern = "\\b\(searchString)\\b"
might fail if a user inputs (text) and wishes to search for it as a whole word. The best way to match whole words is by means of lookarounds disallowing word chars on both ends of the search word:
if isWholeWords {
pattern = "(?<!\\w)" + NSRegularExpression.escapedPatternForString(searchString) + "(?!\\w)"

How modify the word boundary that includes '-' as a word character

I'd like to capture a passcode that is between 6 and 8 digits long.
I'd like to match:
123-4567 and
12-34-56-78
And fail:
1234567890 and 123-456-7890
As it stands I'm using (\\b(?:\\d[-,\\h]?+){5,7}\\d\\b)
This successfully knocks back 1234567890, but gives a partial match on 123-456-7890. Is there a way for the word boundary to include hyphens within it's count?
You can use lookarounds:
(?<!-)\b\d(?:[-,\h]?\d){5,7}(?!-)\b
See the regex demo
Swift regex uses ICU flavor, so both the lookbehind and a lookahead will work. The (?<!-) lookbehind makes sure there is no - before the digit that starts a new word (or after a word boundary), and (?!-) lookahead makes sure there is no - after the 8th digit right at the word boundary.
Do not forget to use double backslashes.
As #AlanMoore suggests, the word boundaries and --lookarounds can be substituted with lookarounds (?<![\w-]) and (?![\w-]). This will make the regex a bit more efficient since there will be only one position to be checked once at the start and end:
(?<![\w-])\d(?:[-,\h]?\d){5,7}(?![\w-])
See another demo
Not an exact literal answer, but an alternative native Swift solution
enum CheckResult {
case Success(String), Failure
}
func checkPassCode(string : String) -> CheckResult
{
let filteredArray = string.characters.filter{ $0 != "-" }.map{ String($0) }
return (6...8).contains(filteredArray.count) ? .Success(filteredArray.joinWithSeparator("")) : .Failure
}
checkPassCode("123-4567") // Success(1234567)
checkPassCode("12-34-56-78") // Success(12345678)
checkPassCode("1234567890") // Failure
checkPassCode("123-456-7890") // Failure

String literal as argument for func within println?

Is there anyway to use a string literal as an argument to a function within a println statement.
func greetings(name: String) -> String {
return "Greetings \(name)!"
}
What I was trying to do: (I tried escaping the quotes around Earthling.)
println("OUTPUT: \(greetings("Earthling"))")
You can alternatively do this:
let name = "Earthling"
println("OUTPUT: \(greetings(name))")
And this works too:
println(greetings("Earthling"))
I tried escaping the quotes in the first example but with no luck, its not super important as its only a test, I was just curious if there was a way to do this, using a function call with a string literal as an argument within a print or println statement that contains other text.
From the Apple docs:
The expressions you write inside parentheses within an interpolated
string cannot contain an unescaped double quote (") or backslash (\),
and cannot contain a carriage return or line feed.
The problem is of course not with println but with the embedding of expressions with quotes in string literals.
Thus
let b = false
let s1 = b ? "is" : "isn't"
let s2 = "it \(b ? "is" : "isn't")" // won't compile
However NSLog as a one-liner'' works quite well here
NSLog("it %#", b ? "is" : "isn't")
Note %#, not %s. Try the latter in a playground to see why.

Resources