如何分割字符串使用正则表达式表达式

3 篇文章 0 订阅
2 篇文章 0 订阅

我有一个字符串"323 ECO Economics Course 451 ENG English Course 789 Mathematical Topography"我想分裂使用正则表达式表达[0-9][0-9][0-9][A-Z][A-Z][A-Z]这个字符串,该函数返回数组:如何分割字符串使用正则表达式表达式

Array = 
["323 ECO Economics Course ", "451 ENG English Course", "789 Mathematical Topography"] 

我怎么会去这样做使用SWIFT ?

编辑 我的问题与链接到的不同。我意识到你可以使用myString.components(separatedBy: "splitting string")快速分割一个字符串问题是这个问题没有解决如何使splitting string成为一个正则表达式。我尝试使用mystring.components(separatedBy: "[0-9][0-9][0-9][A-Z][A-Z][A-Z]", options: .regularExpression),但没有奏效。

我怎样才能让separatedBy:部分的正则表达式?

斯威夫特没有本地正则表达式。但是Foundation提供了NSRegularExpression

import Foundation 

let toSearch = "323 ECO Economics Course 451 ENG English Course 789 MAT Mathematical Topography" 

let pattern = "[0-9]{3} [A-Z]{3}" 
let regex = try! NSRegularExpression(pattern: pattern, options: []) 

// NSRegularExpression works with objective-c NSString, which are utf16 encoded 
let matches = regex.matches(in: toSearch, range: NSMakeRange(0, toSearch.utf16.count)) 

// the combination of zip, dropFirst and map to optional here is a trick 
// to be able to map on [(result1, result2), (result2, result3), (result3, nil)] 
let results = zip(matches, matches.dropFirst().map { Optional.some($0) } + [nil]).map { current, next -> String in 
    let range = current.rangeAt(0) 
    let start = String.UTF16Index(range.location) 
    // if there's a next, use it's starting location as the ending of our match 
    // otherwise, go to the end of the searched string 
    let end = next.map { $0.rangeAt(0) }.map { String.UTF16Index($0.location) } ?? String.UTF16Index(toSearch.utf16.count) 

    return String(toSearch.utf16[start..<end])! 
} 

dump(results) 

运行,将输出

▿ 3 elements 
    - "323 ECO Economics Course " 
    - "451 ENG English Course " 
    - "789 MAT Mathematical Topography" 

您可以使用正则表达式"\\b[0-9]{1,}[a-zA-Z ]{1,}"这扩展从这个answer使用文字,CASEINSENSITIVE或正则表达式搜索字符串的所有范围来获得:

extension String { 
    func ranges(of string: String, options: CompareOptions = .literal) -> [Range<Index>] { 
     var result: [Range<Index>] = [] 
     var start = startIndex 
     while let range = range(of: string, options: options, range: start..<endIndex) { 
      result.append(range) 
      start = range.lowerBound < range.upperBound ? range.upperBound : index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex 
     } 
     return result 
    } 
} 

let inputString = "323 ECO Economics Course 451 ENG English Course 789 Mathematical Topography" 

let courses = inputString.ranges(of: "\\b[0-9]{1,}[a-zA-Z ]{1,}", options: .regularExpression).map{inputString[$0] } 

print(courses) // ["323 ECO Economics Course ", "451 ENG English Course ", "789 Mathematical Topography"] 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值