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

正则表达式在iOS中不起作用,在其他地方可以正常工作,ios,swift,regex,macos,nsregularexpression,Ios,Swift,Regex,Macos,Nsregularexpression,我有一个正则表达式来解析CocoaPods pod文件中的pod名称;我写这篇文章是为了作为漏洞扫描的一部分 可以看出,这个正则表达式在RegexR中工作。但是,在NSRegularExpression中使用时,不会从同一输入返回匹配项 我以前知道NSRegularExpression有点挑剔,但这次我解决不了这个问题 正则表达式是: /(?您不需要在Swift中使用/{pattern}/g语法 这对我来说在操场上很管用 import UIKit import PlaygroundSupport

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

可以看出,这个正则表达式在RegexR中工作。但是,在
NSRegularExpression
中使用时,不会从同一输入返回匹配项

我以前知道
NSRegularExpression
有点挑剔,但这次我解决不了这个问题

正则表达式是:
/(?您不需要在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)
导入UIKit
导入PlaygroundSupport
让podfileString=“”
平台:ios,“11.0”
禁止所有警告!
使用你的框架!
使用模块化标题!
def共享_吊舱
吊舱“CocoaLumberjack/Swift”
pod“PromiseKit”
吊舱'GZIP'
pod'Eureka'=>'4.2.0',:配置=>'Debug'
吊舱“RATreeView”
pod“许可证查看控制器”
终止
目标是“数字时间”吗
吊舱“砖石”
“Instabug”吊舱
吊舱“SAMKeychain”
pod“混合面板”
吊舱“SDWebImage”
吊舱“FLAnimatedImage”
吊舱“SnapKit”,“~>4.0.0”
pod“ObjectMapper”
“迅捷林”吊舱
吊舱“MarqueeLabel/Swift”
阿拉莫菲尔吊舱
pod“PromiseKit/Alamofire”
吊舱“UIColor\U Hex\U Swift”
吊舱“迅捷JSON”
吊舱“英雄”
吊舱“GPUImage”
共享豆荚
终止
目标“DigiMeTests”do
#######斯佩克塔######
吊舱“Specta”,“~>1.0”
pod'Expecta','~>1.0'#Expecta匹配器
吊舱“OHHTTPStubs/Swift”
#####################
共享豆荚
终止
安装后的do安装程序|
installer.pods_project.targets.each do| target|
target.build|u configurations.each do| config|
预处理器\u定义=config.build\u设置['GCC\u预处理器\u定义']
如果预处理器_定义==nil,则预处理器_定义=['$(继承)']
预处理器\u definitions.push'MIXPANEL\u NO\u IFA'如果target.to\u.s.include?'MIXPANEL'
config.build\u设置['GCC\u预处理器\u定义]]=预处理器\u定义
config.build\u设置['CLANG\u ENABLE\u CODE\u COVERAGE']=“否”
终止
终止
终止
"""
let range=NSRange(位置:0,长度:podfileString.utf16.count)

让regex=try!NSRegularExpression(模式:(?
pattern:(?感谢您提供有关iOS中标志的提示。实现上述代码解决了这个问题,不过,为了结束起见,我不应该认为是正则表达式不如我传入的范围那么有效。请注意,我使用的是
NSRangeFromString
,这个答案使用字符串的UTF-16视图创建了一个范围。These显然返回不同的值。我很惊讶UTF-16是必需的,尽管我认为标准是UTF-8。另外,对于多行字符串+1,我以前没有见过。谢谢,是的,utf16显然对特殊字符和表情符号有帮助(我不是这方面的专家,只是在这方面有所了解),很高兴能帮上忙
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)