【问题标题】:Negative look around matching when it shouldn't在不应该的情况下消极环视匹配
【发布时间】:2023-01-02 13:00:00
【问题描述】:

我试图更好地理解正则表达式,以及 this tester 中的环视。

我有以下正则表达式:

class.*(?!(XC))

以及以下测试:

class LocalCardsTests: XCTestCase { // should not match
class LocalTest2 { // Should match

但是,当只有一条线应该匹配时,两条线都匹配。我错过了什么?

【问题讨论】:

  • 在您的情况下,.*LocalCradsTests: XC 匹配,后面没有 XC

标签: regex regex-lookarounds swiftlint


【解决方案1】:

问题是 .* 应该在前瞻中:

class(?!.*XC)

您的正则表达式 class.*(?!(XC))(其中有不必要的括号)匹配“类”和所有剩余字符,因为 (?!XC) 在输入末尾始终为真。

【讨论】:

    猜你喜欢
    • 2019-10-23
    • 2015-12-24
    • 2019-01-31
    • 2017-10-28
    • 1970-01-01
    • 2015-11-07
    • 2014-12-18
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多