fix problem with utf8 after return new string with stringByReplacingMatches apple method
pod 'SwiftRegularExpression'
or
// swift-tools-version:5.2 ... dependencies: [ .package(name: "SwiftRegularExpression", url: "https://github.com/nerzh/swift-regular-expression.git", .upToNextMajor(from: "0.2.0")) ], targets: [ .target( name: "nameProject", dependencies: [ .product(name: "SwiftRegularExpression", package: "SwiftRegularExpression") ] ) ] ...
import SwiftRegularExpression
"hello world"[#"[^\s]+"#] /// => String: "hello" /// => String: "" if no matches
"hello world"[#"[^\s]+"#] /// => Bool: true
"match".regexp(#"(ma)([\s\S]+)"#) /// => Dictionary: [0: "match", 1: "ma", 2:"tch"]
"111 Hello 111".replaceFirst(#"\d+"#, "!!!") /// => String: "!!! Hello 111"
var str = "111 Hello 111" str.replaceFirstSelf(#"\d+"#, "!!!") /// Mutating "!!! Hello 111"
"111 Hello 111".replaceFirst(#"\d+"#) { (value) -> String in return value == "111" ? "???" : value } /// => "??? Hello 111"
var str = "111 Hello 111" str.replaceFirstSelf(#"\d+"#) { (value) -> String in return value == "111" ? "???" : value } /// Mutate to "??? Hello 111"
"111 Hello 111".replace(#"\d+"#, "!!!") /// => "!!! Hello !!!"
var str = "111 Hello 111" str.replaceSelf(#"\d+"#, "!!!") /// Mutate to "!!! Hello !!!"
"111 Hello 222".replace(#"\d+"#) { (value) -> String in return value == "222" ? "111" : value } /// => "111 Hello 111"
var str = "111 Hello 222" str.replaceSelf(#"\d+"#) { (value) -> String in return value == "222" ? "111" : value } /// Mutate to "111 Hello 111"
"23 34".matchesWithRange(#"\d+"#) /// => [Range<String.Index> : "23", Range<String.Index> : "34"]
"23 34".matchesWithRange(#"\d+"#) /// => [[Range<String.Index> : "23", Range<String.Index> : "34"]]
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
swift-regular-expression
Swift REGEXP ( swift regular expression )
Install
or
Usage
regexp: Find String matches
regexp: Return Bool
regexp: Return Dictionary of numbered matches
regexp: Replace First match to value
regexp: Replace All match to values
regexp: Get All Match Ranges