如何在 swift 中转义动态正则表达式?

How to escape a dynamic regex in swift?

所以,我从 API 2f5e28285b5e3c3e28295b5c5d5c5c2e2c3b3a5c7340225d2b285c2e5b5e3c3e28295b5c5d5c5c2e2c3b3a5c7340225d2b292a297c28222e2b2229294028285c5b5b302d395d7b312c337d5c2e5b302d395d7b312c337d5c2e5b302d395d7b312c337d5c2e5b302d395d7b312c337d5d297c28285b612d7a412d5a5c2d302d395d2b5c2e292b5b612d7a412d5a5d7b322c7d2929242f

中得到了一个十六进制字符串

一旦解码为 utf 字符串,这就是形成的正则表达式

/^(([^<>()[\]\.,;:\s@"]+(\.[^<>()[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

根据某些在线正则表达式验证程序,这是一个有效的电子邮件正则表达式。现在问题出现在如何转义这个字符串上。我试过下面的代码

if let data = emailRegex.hexadecimal, let string = String(data: data, encoding: .utf8) {
                guard NSPredicate(format: "SELF MATCHES %@", NSRegularExpression.escapedPattern(for: string))
                    .evaluate(with: email) else {
                        throw ValidationError.invalidInput
                }
                
                isValid = true
            }
            else {
                throw ValidationError.missingInput
            }

这导致以下转义正则表达式:

\/\^\(\(\[\^<>\(\)\[\\]\\\\\.,;:\\s@\"]\+\(\\\.\[\^<>\(\)\[\\]\\\\\.,;:\\s@\"]\+\)\*\)\|\(\"\.\+\"\)\)@\(\(\\\[\[0-9]\{1,3\}\\\.\[0-9]\{1,3\}\\\.\[0-9]\{1,3\}\\\.\[0-9]\{1,3\}]\)\|\(\(\[a-zA-Z\\-0-9]\+\\\.\)\+\[a-zA-Z]\{2,\}\)\)\$\/

以下转义正则表达式会导致正确电子邮件的错误结果,即使是正确的电子邮件也会出现验证错误。任何帮助将不胜感激!

编辑 1: 将代码更新为

let string = String(String(data: data, encoding: .utf8)!.dropFirst().dropLast())

但编译器在以下情况下崩溃 -

使用

((?<!\)(?:\\)*\[(?:\.|[^\]\[])*)\[

替换:\[。参见 regex proof

解释

--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
      \                       '\'
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      \                       '\'
--------------------------------------------------------------------------------
      \                       '\'
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
    \[                       '['
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      \                       '\'
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      [^\]\[]                  any character except: '\]', '\['
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of 
--------------------------------------------------------------------------------
  \[                       '['