使用Swift Regex Builder,如何将包含多个值的捕获语句转换为整数

hjqgdpho  于 2023-08-08  发布在  Swift
关注(0)|答案(1)|浏览(87)

学习Swift Regex Builder我正在处理的字符串包含如下值:x,xxx,xxx。我可以用capture和transform将值捕获为字符串,但我希望输出一个整数。有没有一种方法可以访问捕获块中的Repeat语句所获得的值,并将它们转换为一个整数值?下面是我的测试函数。

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
        
//#########################################################################################
        Capture {
            Repeat(1...3) {
                One(.digit)
            }
            ","
            Repeat(count: 3) {
                One(.digit)
            }
            ","
            Repeat(count: 3) {
                One(.digit)
            }
        } transform: {
            String($0).    // this is where I would like to output an integer
        }
//#########################################################################################
        
        EndingSeperator
        
    }
    
    for match in stringData.matches(of: tradingDayPattern) {
        
        let (_, date, _, _, _, close, _, v1) = match.output
        print("\(date) - \(close) - \(v1)")
    }
} // end testing function

字符串

deyfvvtc

deyfvvtc1#

如果它总是一个格式良好的数字(即逗号分隔符,每3位数,并没有像1,2,3等),那么你可以使用一个IntegerFormatStyle,类似于你在捕获日期时所做的。

Capture(IntegerFormatStyle<Int>(locale: Locale(identifier: "en_US_POSIX")))

字符串
您应该指定适当的区域设置。
如果数字可能是像1,2,3这样奇怪的数字,那么你可以替换逗号并将其转换为Int

transform: {
    Int($0.replacing(",", with: ""))
}


请注意,这会产生一个可选的Int?

相关问题