import Foundation
let pattern = #"(^|[+-]?\d+\.\d*)$"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
TICKER|LAST_UPDE|PX_LAST
EE0002M Index|20211231|-0.58814
US0002M Index||
USB3MAY Index|20220502|0.8x9
SOFRRATE Index|20220505|c0.79
FEDL01 Index|20220505|0.83
US0001M Index|2022050|0.84486
FDTR Index|20220505|1
US0003M Index|20220505|1.37071
PRIME Index|20220505|4
TICKER|LAST_UPDATE|PX_LAST
EE0002M Index|20211231|-0.58814
US0002M Index|20211231|0.1525
USB3MAY Index|20220502|0.89
PRIME Index|20220503|3.5
SOFRRATE Index|20220504|0.3
FEDL01 Index|20220504|0.33
US0001M Index|20220504|0.84514
FDTR Index|20220504|0.5
US0003M Index|20220504|1.40614
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression