There is a regex for email validation in java that works perfectly in android. Can someone help me convert it to swift?

huangapple 未分类评论60阅读模式
标题翻译

There is a regex for email validation in java that works perfectly in android. Can someone help me convert it to swift?

问题

以下是你要翻译的内容:

在 Kotlin 中的正则表达式不允许包含空格、表情等。我希望在 Swift 中使用这个正则表达式,但它的工作方式不太一样,需要进行转换。我已经尝试过,但无法实现正确的转换。帮助将不胜感激。

在在线 Kotlin Playground 中测试的代码:

import java.util.regex.*

fun main() {
    
    println(isValidEmailAddress(" abc@gmail.com"))
    // 返回 false
    println(isValidEmailAddress("a😃bc@gmail.com"))
    // 返回 false

}

val EMAIL_PATTERN = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
            "\\@" +
            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
            "(" +
                "\\." +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
            ")+")

public fun isValidEmailAddress(email: String): Boolean {
        return EMAIL_PATTERN.matcher(email).matches()
    }

在在线 Swift Playground 中测试的代码:

import Foundation

extension String {
    func isValidEmail() -> Bool {
        let regex = try! NSRegularExpression(pattern: "[a-zA-Z0-9+._%-+]{1,256}@[a-zA-Z0-9][a-zA-Z0-9-]{0,64}+\\.[a-zA-Z0-9][a-zA-Z0-9-]{0,25}+", options: .caseInsensitive)
        return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: count)) != nil
    }
}

var str = " abc@gmail.com".isValidEmail()
print(str)
// 返回 true
str = "a😃bc@gmail.com".isValidEmail()
print(str)
// 返回 true

在根据建议进行更改后的回答:

import Foundation

extension String {
    func isValidEmail() -> Bool {
        let regex = try! NSRegularExpression(pattern: "^[a-zA-Z0-9+._%-]{1,256}@[a-zA-Z0-9][a-zA-Z0-9-]{0,64}+(\\.[a-zA-Z0-9][a-zA-Z0-9-]{0,25})+$", options: .caseInsensitive)
        return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count)) != nil
    }
}

var str = " abcgmail.com".isValidEmail()
print(str)
// 返回 false
str = "a😃c@gmail.com".isValidEmail()
print(str)
// 返回 false
英文翻译

The regex in kotlin does not allow white spaces, emojis etc. I want this regex to be used in swift but the regex does not work exactly and needs to be converted. I have tried but I am unable to achieve proper conversion. Help would be appreciated

Code tested in online kotlin playground

Kotlin Playground code:

import java.util.regex.*

fun main() {
    
    println(isValidEmailAddress(" abc@gmail.com"))
    // returns false
    println(isValidEmailAddress("a😃bc@gmail.com"))
    // returns false

}

val EMAIL_PATTERN = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
            "\\@" +
            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
            "(" +
                "\\." +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
            ")+")

public fun isValidEmailAddress(email: String): Boolean {
        return EMAIL_PATTERN.matcher(email).matches()
    }

Code tested in online swift playground

Swift Playground code:

import Foundation

extension String {
    func isValidEmail() -> Bool {
        let regex = try! NSRegularExpression(pattern: "[a-zA-Z0-9+._%-+]{1,256}@[a-zA-Z0-9][a-zA-Z0-9-]{0,64}+\\.[a-zA-Z0-9][a-zA-Z0-9-]{0,25}+", options: .caseInsensitive)
        return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: count)) != nil
    }
}

var str = " abc@gmail.com".isValidEmail()
print(str)
// returns true
str = "a😃bc@gmail.com".isValidEmail()
print(str)
// returns true

Answer after following suggested changes:

import Foundation

extension String {
    func isValidEmail() -> Bool {
        let regex = try! NSRegularExpression(pattern: "^[a-zA-Z0-9+._%-]{1,256}@[a-zA-Z0-9][a-zA-Z0-9-]{0,64}+(\\.[a-zA-Z0-9][a-zA-Z0-9-]{0,25})+$", options: .caseInsensitive)
        return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count)) != nil
    }
}

var str = " abcgmail.com".isValidEmail()
print(str)
// returns false
str = "a😃c@gmail.com".isValidEmail()
print(str)
// returns false

huangapple
  • 本文由 发表于 2020年3月16日 15:38:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/60701963.html
匿名

发表评论

匿名网友

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

确定