正则表达式在 iOS 中不工作,在其他地方工作正常

Regular expression not working in iOS, works fine elsewhere

我有一个正则表达式来解析 CocoaPods podfile 中的 pod 名称;我写这篇文章是为了用作漏洞扫描的一部分。

这个RegEx可以看出是在RegexR中工作的,可见here。但是,在 NSRegularExpression 中使用时,不会从同一输入返回任何匹配项。

我以前就知道NSRegularExpression有点挑剔,但这次我无法解决问题。

正则表达式是: /(?<=pod ')[A-Za-z/]+(?=')/g

我的Swift代码是:

guard let podNameRegex = try? NSRegularExpression(pattern: "/(?<=pod ')[A-Za-z/]+(?=')/g", options: [.useUnicodeWordBoundaries]) else {
    fputs("Failed to instantiate pod name regex.", stderr)
    throw NSError()
}

let podfilePodNameMatches = podNameRegex.matches(in: podfileString, options: [], range: NSRangeFromString(podfileString))
let matchStrings = podfilePodNameMatches.map({ (podfileString as NSString).substring(with: [=11=].range) })

但是,没有返回任何匹配项。

有谁知道问题出在哪里? TIA

您不需要在 Swift 中使用 /{pattern}/g 语法。

这在操场上对我有用

import UIKit
import PlaygroundSupport

let podfileString = """
platform :ios, '11.0'

inhibit_all_warnings!
use_frameworks!
use_modular_headers!

def shared_pods
    pod 'CocoaLumberjack/Swift'
    pod 'PromiseKit'
    pod 'GZIP'
    pod 'Eureka' => '4.2.0', :configuration => 'Debug'
    pod 'RATreeView'
    pod 'LicensesViewController'
end

target 'DigiMe' do
    pod 'Masonry'
    pod 'Instabug'
    pod 'SAMKeychain'
    pod 'Mixpanel'
    pod 'SDWebImage'
    pod 'FLAnimatedImage'
    pod 'SnapKit', '~> 4.0.0'
    pod 'ObjectMapper'
    pod 'SwiftLint'
    pod 'MarqueeLabel/Swift'
    pod 'Alamofire'
    pod 'PromiseKit/Alamofire'
    pod 'UIColor_Hex_Swift'
    pod 'SwiftyJSON'
    pod 'Hero'
    pod 'GPUImage'
    shared_pods
end

target 'DigiMeTests' do

    ####### SPECTA ######
    pod 'Specta',       '~> 1.0'
    pod 'Expecta',      '~> 1.0'   # expecta matchers
    pod 'OHHTTPStubs/Swift'
    #####################

    shared_pods
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            preprocessor_definitions = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
            preprocessor_definitions = ['$(inherited)'] if preprocessor_definitions == nil
            preprocessor_definitions.push 'MIXPANEL_NO_IFA' if target.to_s.include? 'Mixpanel'
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = preprocessor_definitions
            config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
        end
    end
end
"""

let range = NSRange(location: 0, length: podfileString.utf16.count)
let regex = try! NSRegularExpression(pattern: "(?<=pod ')[A-Za-z/]+(?=')")
let matches = regex.matches(in: podfileString, options: [], range: range)
let matchStrings = matches.map({ (podfileString as NSString).substring(with: [=10=].range) })
print(matchStrings)

输出:

["CocoaLumberjack/Swift", "PromiseKit", "GZIP", "Eureka", "RATreeView", "LicensesViewController", "Masonry", "Instabug", "SAMKeychain", "Mixpanel", "SDWebImage", "FLAnimatedImage", "SnapKit", "ObjectMapper", "SwiftLint", "MarqueeLabel/Swift", "Alamofire", "PromiseKit/Alamofire", "SwiftyJSON", "Hero", "GPUImage", "Specta", "Expecta", "OHHTTPStubs/Swift"]