正则表达式在iOS中不起作用,在其他地方也可以正常工作

时间:2019-03-05 15:11:32

标签: ios swift regex macos nsregularexpression

我有一个正则表达式来解析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: $0.range) })

但是,不会返回任何匹配项。

有人知道问题出在哪里吗? TIA

1 个答案:

答案 0 :(得分:1)

您无需在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: $0.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“]