【问题标题】:How to escape a dynamic regex in swift?如何快速逃避动态正则表达式?
【发布时间】:2022-01-24 07:00:54
【问题描述】:

所以,我从 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())

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

【问题讨论】:

  • 你不需要转义它,它是一个正则表达式,已经转义了。要使其在 Swift 中工作,您需要删除第一个和最后一个 /
  • @WiktorStribiżew 我认为你应该写下你的评论作为答案,因为它就是答案。
  • @WiktorStribiżew 请检查更新
  • 是的,您使用的正则表达式是 JavaScript ECMAScript 兼容的,并且在 Swift 中,使用了 ICU 正则表达式库。您需要在字符类中转义 []。现在,问题是您是想将任何 ECMAScript 模式转换为 ICU,还是只使用这个电子邮件正则表达式验证正则表达式?没有通过一些 API 获取它?
  • @WiktorStribiżew 我想我必须将 ECMAScript 模式转换为 ICU,因为我收到的十六进制字符串是动态的

标签: ios swift regex nspredicate nsregularexpression


【解决方案1】:

使用

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

替换:$1\\[。见regex proof

解释

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    (?<!                     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 \1
--------------------------------------------------------------------------------
  \[                       '['

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2016-01-15
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2018-08-29
    相关资源
    最近更新 更多