排除德语umlaut从给定的正则表达式匹配中。

huangapple go评论62阅读模式
英文:

Exclude German umlaut from Given Regex Match

问题

我有一个密码匹配要求,我不想在密码验证中包含德语umlaut。当前的匹配标准是:

  1. 一个小写字母
  2. 一个大写字母
  3. 一个数字
  4. 至少 10 个字符

我创建了一个如下的正则表达式:

func matchPasswordWith(_ text: String) -> Bool {
    let predicate = NSPredicate(format: "SELF MATCHES %@", "^(?=.*?[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{10,}$")
    return predicate.evaluate(with: text)
}

但是当用户输入任何umlaut(如 ü, ä, Ä, ß)时,上面的正则表达式会返回 true,但我想排除umlaut的匹配。

请建议我在这里做错了什么。

英文:

I have a password match requirment where I do not want to include German umlaut in password validation. Current match criteria is:

  1. one lower case
  2. one upper case
  3. One digit
  4. minimum 10 chars

I created a regex as below:

func matchPasswordWith(_ text: String) -> Bool {
    let predicate = NSPredicate(format: "SELF MATCHES %@","^(?=.*?[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{10,}$")
    return predicate.evaluate(with: text)
}

But when a user enters any umlaut (e.g. ü, ä, Ä, ß), the above regex returns true but I want to exclude umlaut match.

Please suggest what am I doing wrong here.

答案1

得分: 1

你可以使用字符类交集来否定字符类中的值。

语法是 &&[],必须放在字符类内部,位于末尾。

此外,你可以简化你的模式。

(?i)^[a-z\d&&[^äöüÄÖÜß]]{10,}$

(?i) 将启用不区分大小写模式。

以下是一些示例。

这些将匹配。

abcdefghij
1234567890
AbCdE12345

而这些将不匹配。

äbcdefghij
12345
ÄbCdE12345
英文:

You can negate values from a character class, using a character class intersection.

The syntax is &&[], and must be placed within the character class, at the end.

Additionally, you can reduce your pattern.

(?i)^[a-z\d&&[^äöüÄÖÜß]]{10,}$

The (?i) will toggle-on case-insensitive mode.

Here are some examples.

These will match.

abcdefghij
1234567890
AbCdE12345

And, these will not.

äbcdefghij
12345
ÄbCdE12345

答案2

得分: 0

你可以通过包括你想要的所有非umlaut字符来排除umlauts在密码匹配中。将 (?=.*[a-zA-Z]).{10,}$ 替换为类似 [A-Za-z0-9]{10,}$ 的内容。

func matchPasswordWith(_ text: String) -> Bool {
    let predicate = NSPredicate(format: "SELF MATCHES %@", "^(?=.*?[0-9])(?=.*[a-z])(?=.*[A-Z])[A-Za-z0-9]{10,}$")
    return predicate.evaluate(with: text)
}

[A-Za-z0-9]{10,} 检查所有字符是否是字母或数字,且至少有10个字符。显然,它也排除了其他特殊字符,但你可以随时将这些字符添加到你的字符集中,例如:[A-Za-z0-9@#$%^&*]{10,},或者任何其他你想要包含的字符。

英文:

You could exclude umlauts in your password matching by including all the non-umlaut characters you want to have. Replacing (?=.*[a-zA-Z]).{10,}$ with something like [A-Za-z0-9]{10,}$

func matchPasswordWith(_ text: String) -> Bool {
    let predicate = NSPredicate(format: "SELF MATCHES %@","^(?=.*?[0-9])(?=.*[a-z])(?=.*[A-Z])[A-Za-z0-9]{10,}$")
    return predicate.evaluate(with: text)
}

[A-Za-z0-9]{10,} Checks that all characters are either a letter or a number and that there are at least 10 characters. Obviously it also excludes other special characters, but you could always add those to your character set like: [A-Za-z0-9@#$%^&*]{10,}. or whatever other characters you want to include.

huangapple
  • 本文由 发表于 2023年6月15日 21:25:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定