You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Swift Regex Builder将多值捕获语句转换为整数

解决Swift Regex Builder中带千分位逗号的数值转整数问题

你可以直接在捕获的transform闭包里处理带逗号的字符串,去掉所有千分位逗号后转换为整数。这里推荐用TryCapture替代普通Capture,避免因格式异常导致转换失败而中断匹配。

修改你代码中对应的捕获块如下:

TryCapture {
    Repeat(1...3) {
        One(.digit)
    }
    ","
    Repeat(count: 3) {
        One(.digit)
    }
    ","
    Repeat(count: 3) {
        One(.digit)
    }
} transform: { match in
    let numericString = String(match).replacingOccurrences(of: ",", with: "")
    return Int(numericString)
}

完整修改后的测试函数

func Test()  {
    
    let stringData = "<tr class=BdT Bdc($seperatorColor) Ta(end) Fz(s) Whs(nw)><td class=Py(10px) Ta(start) Pend(10px)><span>Jun 30, 2023</span></td><td class=Py(10px) Pstart(10px)><span>405.40</span></td><td class=Py(10px) Pstart(10px)><span>408.22</span></td><td class=Py(10px) Pstart(10px)><span>405.29</span></td><td class=Py(10px) Pstart(10px)><span>407.28</span></td><td class=Py(10px) Pstart(10px)><span>407.28</span></td><td class=Py(10px) Pstart(10px)><span>5,160,100</span></td></tr><tr class=BdT Bdc($seperatorColor) Ta(end) Fz(s) Whs(nw)><td class=Py(10px) Ta(start) Pend(10px)><span>Jun 29, 2023</span></td><td class=Py(10px) Pstart(10px)><span>400.60</span></td><td class=Py(10px) Pstart(10px)><span>402.67</span></td><td class=Py(10px) Pstart(10px)><span>400.19</span></td><td class=Py(10px) Pstart(10px)><span>402.51</span></td><td class=Py(10px) Pstart(10px)><span>402.51</span></td><td class=Py(10px) Pstart(10px)><span>3,914,800</span></td></tr><tr class=BdT Bdc($seperatorColor) Ta(end) Fz(s) Whs(nw)><td class=Py(10px) Ta(start) Pend(10px)><span>Jun 28, 2023</span></td><td class=Py(10px) Pstart(10px)><span>401.35</span></td><td class=Py(10px) Pstart(10px)><span>403.49</span></td><td class=Py(10px) Pstart(10px)><span>400.71</span></td><td class=Py(10px) Pstart(10px)><span>402.55</span></td><td class=Py(10px) Pstart(10px)><span>400.97</span></td><td class=Py(10px) Pstart(10px)><span>4,320,700</span></td></tr>"
    
    let StartingSeperator = Regex {"<td class=Py(10px) Ta(start) Pend(10px)><span>"}
    let InterValueSeperator = Regex {"</span></td><td class=Py(10px) Pstart(10px)><span>"}
    let EndingSeperator = Regex {"</span>"}
    
    let CaptureDollarValue = Regex {
        TryCapture {
            OneOrMore(.digit)
            "."
            Repeat(.digit, count: 2)
        } transform: {
            Double($0)
        }
    }
    
    let tradingDayPattern = Regex {
        
        StartingSeperator
        
        Capture(.date(format: "\(month: .abbreviated) \(day: .twoDigits), \(year: .extended(minimumLength: 4))", locale: Locale(identifier: "en_US_POSIX") , timeZone: .gmt))
        InterValueSeperator
        
        CaptureDollarValue
        InterValueSeperator
        
        CaptureDollarValue
        InterValueSeperator
        
        CaptureDollarValue
        InterValueSeperator
        
        CaptureDollarValue
        InterValueSeperator
        
        CaptureDollarValue
        InterValueSeperator
        
        // 修改后的捕获转换块
        TryCapture {
            Repeat(1...3) {
                One(.digit)
            }
            ","
            Repeat(count: 3) {
                One(.digit)
            }
            ","
            Repeat(count: 3) {
                One(.digit)
            }
        } transform: { match in
            let numericString = String(match).replacingOccurrences(of: ",", with: "")
            return Int(numericString)
        }
        
        EndingSeperator
        
    }
    
    for match in stringData.matches(of: tradingDayPattern) {
        // 注意这里v1的类型现在是Int?,可以根据需要解包
        let (_, date, _, _, _, close, _, v1) = match.output
        print("\(date) - \(close) - \(v1 ?? 0)")
    }
} // end testing function

关键说明

  • 捕获块中的Repeat语句会返回整个匹配的子字符串(比如"5,160,100"),你可以直接将其转换为String后处理。
  • 使用replacingOccurrences(of:with:)去掉所有逗号,得到纯数字字符串后转成Int。
  • 用TryCapture替代Capture,确保如果字符串转换失败(比如格式异常)时,整个匹配不会崩溃,而是跳过该条目。

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 05:43:08