| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
654 バイト追加 、 2021年4月6日 (火) 15:03
*https://www.hackingwithswift.com/example-code/strings/nsregularexpression-how-to-match-regular-expressions-in-strings
*https://www.2nd-walker.com/2020/04/21/swift-how-to-use-nsregularexpression/
<pre>
do {
let pattern = #"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"#
let regex = try NSRegularExpression(pattern: pattern, options:[])
for subinput in input.split(separator: "\r\n") {
let line = String(subinput)
let maches = regex.matches(in: line, options: [], range: _NSRange(0..<line.count))
print(line)
for mach in maches {
for i in 0 ..< mach.numberOfRanges {
let start = line.index(line.startIndex, offsetBy: mach.range(at: i).location)
let end = line.index(start, offsetBy: mach.range(at: i).length)
let text = String(line[start..<end])
print(text)
}
}
}
} catch {
print("RegEx fail.")
}
</pre>
==[[SwiftUI]]==

案内メニュー