How to decode String from percent encoded windowsCP1251?
replacingPercentEscapes(using: String.Encoding.windowsCP1251) now is obsolete
removingPercentEncoding uses utf8
I have found the solution. It works for me. Welcome to refactor my example.
extension String {
func removingPercentEncoding(using encoding: String.Encoding) -> String {
let firstChar = self.first
let percentCharacter = Character("%")
var encodedPrefix: String.SubSequence? = nil
var encodedSuffix = self
if firstChar != percentCharacter {
if let indexOfFirstPercentChar = index(of: percentCharacter) {
encodedPrefix = self[..<indexOfFirstPercentChar]
encodedSuffix = String(self[indexOfFirstPercentChar...])
} else {
//no % char at all. Nothing encoded
return self
}
}
let substrings = encodedSuffix.components(separatedBy: "%")
let arr = substrings.map{ substring -> (String) in
switch substring.count {
case let count where count < 2:
return substring
case let count where count == 2:
let decodedArr = substring.hexa2Bytes
let data = Data(decodedArr)
if let decodedStr = String(data: data, encoding: encoding) {
return decodedStr
}
return substring
default: //>2
let thirdSymbolIndex = index(startIndex, offsetBy: 2)
let firstTwo = substring[..<thirdSymbolIndex]
let furhter = substring[thirdSymbolIndex...]
let decodedArr = String(firstTwo).hexa2Bytes
let data = Data(decodedArr)
if let decodedStr = String(data: data, encoding: encoding) {
return decodedStr + furhter
}
return substring
}
}
let result = arr.joined()
return String(encodedPrefix ?? "") + result
}
var hexa2Bytes: [UInt8] {
let hexa = Array(characters)
return stride(from: 0, to: characters.count, by: 2).flatMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
}
}
A little example which is expected to work with multi-byte string encodings.
extension UInt8 {
//returns 0...15 when '0'...'9', 'A'...'F', 'a'...'f', otherwise returns nil
var hexValue: UInt8? {
if UInt8(ascii: "0") <= self && self <= UInt8(ascii: "9") {
return self - UInt8(ascii: "0")
} else if UInt8(ascii: "A") <= self && self <= UInt8(ascii: "F") {
return self - UInt8(ascii: "A") + 10
} else if UInt8(ascii: "a") <= self && self <= UInt8(ascii: "f") {
return self - UInt8(ascii: "a") + 10
} else {
return nil
}
}
}
extension String {
func removingPercentEncoding(using encoding: String.Encoding) -> String? {
guard let percentEncodedData = self.data(using: encoding) else {return nil}
var byteIterator = percentEncodedData.makeIterator()
var percentDecodedData = Data()
while let b0 = byteIterator.next() {
guard b0 == UInt8(ascii: "%"), let b1 = byteIterator.next() else {
//Non percent character
percentDecodedData.append(b0)
continue
}
guard let h1 = b1.hexValue, let b2 = byteIterator.next() else {
//Keep it as is, when invalid hex-sequece appeared
percentDecodedData.append(b0)
percentDecodedData.append(b1)
continue
}
guard let h2 = b2.hexValue else {
//Keep it as is, when invalid hex-sequece appeared
percentDecodedData.append(b0)
percentDecodedData.append(b1)
percentDecodedData.append(b2)
continue
}
percentDecodedData.append((h1<<4) + h2)
}
return String(data: percentDecodedData, encoding: encoding)
}
}
In my opinion, you should think non-UTF8 percent encoding now is obsolete, and should fix the part which is generating the percent encoded string with CP1251.
Related
Having no luck creating a partial range in Swift 4
import Foundation
public extension String {
public var URLScheme: String? {
guard let schemeRange = self.range(of: "://") else { return nil }
return self.substring(to: schemeRange.lowerBound)
}
public var URLPortNumber: Int {
guard let portRange = self.range(of: ":", options: .backwards) else { return -1 }
let startIndex = self.index(portRange.upperBound, offsetBy: 0)
let endIndex = self.index(portRange.upperBound, offsetBy: 2)
guard self[startIndex...endIndex] != "//" else { return -1 }
return Int(self.substring(from: portRange.upperBound))!
}
public var URLHost: String {
var host = self
if let scheme = self.URLScheme {
host = host.substring(from: self.index(self.startIndex, offsetBy: (scheme + "://").characters.count))
}
if let portRange = host.range(of: ":") {
host = host.substring(to: portRange.lowerBound)
}
return host
}
}
Also after reading the documentation on Substrings, I am still less than clear on their benefit. Has anyone used them for URLs?
Even the syntax is less succinct than dot notation.
This seems to work!
import Foundation
public extension String {
public var URLScheme: String? {
guard let schemeRange = self.range(of: "://") else { return nil }
return String(describing: schemeRange.lowerBound)
}
public var URLPortNumber: Int {
guard let portRange = self.range(of: ":", options: .backwards) else { return -1 }
let startIndex = self.index(portRange.upperBound, offsetBy: 0)
let endIndex = self.index(portRange.upperBound, offsetBy: 2)
guard self[startIndex...endIndex] != "//" else { return -1 }
return Int(String(describing: portRange.upperBound))!
}
public var URLHost: String {
var host = self
if let scheme = self.URLScheme {
host = String(describing: self.index(self.startIndex, offsetBy: (scheme + "://").characters.count))
}
if let portRange = host.range(of: ":") {
host = String(describing: portRange.lowerBound)
}
return host
}
}
I have a string which contains binary digits. How to separate it in to pairs of digits?
Suppose the string is:
let x = "11231245"
I want to add a separator such as ":" (i.e., a colon) after each 2 characters.
I would like the output to be:
"11:23:12:45"
How could I do this in Swift ?
Swift 5.2 • Xcode 11.4 or later
extension Collection {
func unfoldSubSequences(limitedTo maxLength: Int) -> UnfoldSequence<SubSequence,Index> {
sequence(state: startIndex) { start in
guard start < endIndex else { return nil }
let end = index(start, offsetBy: maxLength, limitedBy: endIndex) ?? endIndex
defer { start = end }
return self[start..<end]
}
}
func every(n: Int) -> UnfoldSequence<Element,Index> {
sequence(state: startIndex) { index in
guard index < endIndex else { return nil }
defer { let _ = formIndex(&index, offsetBy: n, limitedBy: endIndex) }
return self[index]
}
}
var pairs: [SubSequence] { .init(unfoldSubSequences(limitedTo: 2)) }
}
extension StringProtocol where Self: RangeReplaceableCollection {
mutating func insert<S: StringProtocol>(separator: S, every n: Int) {
for index in indices.every(n: n).dropFirst().reversed() {
insert(contentsOf: separator, at: index)
}
}
func inserting<S: StringProtocol>(separator: S, every n: Int) -> Self {
.init(unfoldSubSequences(limitedTo: n).joined(separator: separator))
}
}
Testing
let str = "112312451"
let final0 = str.unfoldSubSequences(limitedTo: 2).joined(separator: ":")
print(final0) // "11:23:12:45:1"
let final1 = str.pairs.joined(separator: ":")
print(final1) // "11:23:12:45:1"
let final2 = str.inserting(separator: ":", every: 2)
print(final2) // "11:23:12:45:1\n"
var str2 = "112312451"
str2.insert(separator: ":", every: 2)
print(str2) // "11:23:12:45:1\n"
var str3 = "112312451"
str3.insert(separator: ":", every: 3)
print(str3) // "112:312:451\n"
var str4 = "112312451"
str4.insert(separator: ":", every: 4)
print(str4) // "1123:1245:1\n"
I'll go for this compact solution (in Swift 4) :
let s = "11231245"
let r = String(s.enumerated().map { $0 > 0 && $0 % 2 == 0 ? [":", $1] : [$1]}.joined())
You can make an extension and parameterize the stride and the separator so that you can use it for every value you want (In my case, I use it to dump 32-bit space-operated hexadecimal data):
extension String {
func separate(every stride: Int = 4, with separator: Character = " ") -> String {
return String(enumerated().map { $0 > 0 && $0 % stride == 0 ? [separator, $1] : [$1]}.joined())
}
}
In your case this gives the following results:
let x = "11231245"
print (x.separate(every:2, with: ":")
$ 11:23:12:45
Swift 5.3
/// Adds a separator at every N characters
/// - Parameters:
/// - separator: the String value to be inserted, to separate the groups. Default is " " - one space.
/// - stride: the number of characters in the group, before a separator is inserted. Default is 4.
/// - Returns: Returns a String which includes a `separator` String at every `stride` number of characters.
func separated(by separator: String = " ", stride: Int = 4) -> String {
return enumerated().map { $0.isMultiple(of: stride) && ($0 != 0) ? "\(separator)\($1)" : String($1) }.joined()
}
Short and simple, add a let or two if you want
extension String {
func separate(every: Int, with separator: String) -> String {
return String(stride(from: 0, to: Array(self).count, by: every).map {
Array(Array(self)[$0..<min($0 + every, Array(self).count)])
}.joined(separator: separator))
}
}
let a = "separatemepleaseandthankyou".separate(every: 4, with: " ")
a is
sepa rate mepl ease andt hank you
Its my code in swift 4
let x = "11231245"
var newText = String()
for (index, character) in x.enumerated() {
if index != 0 && index % 2 == 0 {
newText.append(":")
}
newText.append(String(character))
}
print(newText)
Outputs 11:23:12:45
My attempt at that code would be:
func insert(seperator: String, afterEveryXChars: Int, intoString: String) -> String {
var output = ""
intoString.characters.enumerate().forEach { index, c in
if index % afterEveryXChars == 0 && index > 0 {
output += seperator
}
output.append(c)
}
return output
}
insert(":", afterEveryXChars: 2, intoString: "11231245")
Which outputs
11:23:12:45
let y = String(
x.characters.enumerate().map() {
$0.index % 2 == 0 ? [$0.element] : [$0.element, ":"]
}.flatten()
)
A simple One line of code for inserting separater ( Swift 4.2 ):-
let testString = "123456789"
let ansTest = testString.enumerated().compactMap({ ($0 > 0) && ($0 % 2 == 0) ? ":\($1)" : "\($1)" }).joined() ?? ""
print(ansTest) // 12:34:56:78:9
Swift 4.2.1 - Xcode 10.1
extension String {
func insertSeparator(_ separatorString: String, atEvery n: Int) -> String {
guard 0 < n else { return self }
return self.enumerated().map({String($0.element) + (($0.offset != self.count - 1 && $0.offset % n == n - 1) ? "\(separatorString)" : "")}).joined()
}
mutating func insertedSeparator(_ separatorString: String, atEvery n: Int) {
self = insertSeparator(separatorString, atEvery: n)
}
}
Usage
let testString = "11231245"
let test1 = testString.insertSeparator(":", atEvery: 2)
print(test1) // 11:23:12:45
var test2 = testString
test2.insertedSeparator(",", atEvery: 3)
print(test2) // 112,312,45
I'm little late here, but i like to use regex like in this:
extension String {
func separating(every: Int, separator: String) -> String {
let regex = #"(.{\#(every)})(?=.)"#
return self.replacingOccurrences(of: regex, with: "$1\(separator)", options: [.regularExpression])
}
}
"111222333".separating(every: 3, separator: " ")
the output:
"111 222 333"
extension String{
func separate(every: Int) -> [String] {
return stride(from: 0, to: count, by: every).map {
let ix0 = index(startIndex, offsetBy: $0);
let ix1 = index(after:ix0);
if ix1 < endIndex {
return String(self[ix0...ix1]);
}else{
return String(self[ix0..<endIndex]);
}
}
}
/// or O(1) implementation (without count)
func separate(every: Int) -> [String] {
var parts:[String] = [];
var ix1 = startIndex;
while ix1 < endIndex {
let ix0 = ix1;
var n = 0;
while ix1 < endIndex && n < every {
ix1 = index(after: ix1);
n += 1;
}
parts.append(String(self[ix0..<ix1]));
}
return parts;
}
"asdf234sdf".separate(every: 2).joined(separator: ":");
A simple String extension that doesn't require the original string to be a multiple of the step size (increment):
extension String {
func inserted(_ newElement: Character,atEach increment:Int)->String {
var newStr = self
for indx in stride(from: increment, to: newStr.count, by: increment).reversed() {
let index = String.Index(encodedOffset: indx)
newStr.insert(newElement, at: index)
}
return newStr
}
}
let temp: String = "0xffeeffff"
How to convert above String to UInt32, because I need to store it in the bitmap which only accept UInt32
Remove "0x" from your string to convert it to UInt32:
let temp = "0xffeeffff"
let result = UInt32(String(temp.characters.dropFirst(2)), radix: 16)
hope this is help you...
extension String {
func toUInt() -> UInt? {
if contains(self, "-") {
return nil
}
return self.withCString { cptr -> UInt? in
var endPtr : UnsafeMutablePointer<Int8> = nil
errno = 0
let result = strtoul(cptr, &endPtr, 10)
if errno != 0 || endPtr.memory != 0 {
return nil
} else {
return result
}
}
}
}
I am new to swift and I am trying to count the different characters in a string but my code returns the value for the whole String
for example:
var string aString = "aabb"
aString.characters.count() //returns 5
counter = 0
let a = "a"
for a in aString.characters {
counter++
} //equally returns 5
Can somebody explain why this is happening and how I could count the different chars?
It looks there is some confusion about what you really need.
I tried to answer to the 5 most likely interpretations.
var word = "aabb"
let numberOfChars = word.characters.count // 4
let numberOfDistinctChars = Set(word.characters).count // 2
let occurrenciesOfA = word.characters.filter { $0 == "A" }.count // 0
let occurrenciesOfa = word.characters.filter { $0 == "a" }.count // 2
let occurrenciesOfACaseInsensitive = word.characters.filter { $0 == "A" || $0 == "a" }.count // 2
print(occurrenciesOfA)
print(occurrenciesOfa)
print(occurrenciesOfACaseInsensitive)
check this
var aString = "aabb"
aString.characters.count // 4
var counter = 0
let a = "a" // you newer use this in your code
for thisIsSingleCharacterInStringCharactersView in aString.characters {
counter++
}
print(counter) // 4
it simply increase your counter for each character
to calculate number of different characters in you string, you probably can use something 'more advanced', like in next example
let str = "aabbcsdfaewdsrsfdeewraewd"
let dict = str.characters.reduce([:]) { (d, c) -> Dictionary<Character,Int> in
var d = d
let i = d[c] ?? 0
d[c] = i+1
return d
}
print(dict) // ["b": 2, "a": 4, "w": 3, "r": 2, "c": 1, "s": 3, "f": 2, "e": 4, "d": 4]
You code is quite faulty: it should probably start with
let aString = "aabb"
The solutions is to get the characters, put them into a set (unique) and then counting the members of the set:
let differentChars = Set(aString.characters).count
Correctly returns
2
The characters property is deprecated, you can use components(separatedBy:) to find how many characters in a String. eg,
extension String {
public func numberOfOccurrences(_ string: String) -> Int {
return components(separatedBy: string).count - 1
}
}
let aString = "aabbaa"
let aCount = aString.numberOfOccurrences("a") // aCount = 4
Updated #Luca Angeletti's answer for Swift5.3 because characters property is unavailable in newer swift version.
var word = "aabb"
let numberOfChars = word.count // 4
let numberOfDistinctChars = Set(word).count // 2
let occurrenciesOfA = word.filter { $0 == "A" }.count // 0
let occurrenciesOfa = word.filter { $0 == "a" }.count // 2
let occurrenciesOfACaseInsensitive = word.filter { $0 == "A" || $0 == "a" }.count // 2
print(numberOfChars)
print(numberOfDistinctChars)
print(occurrenciesOfA)
print(occurrenciesOfa)
print(occurrenciesOfACaseInsensitive)
Construct a dictionary out of a sequence of (key, value) pairs. If we can guarantee that the keys are unique, we can use Dictionary(uniqueKeysWithValues:).
func characterFrequencies(of string: String) -> Dictionary<String.Element, Int> {
let frequencyPair = string.map { ($0, 1) }
return Dictionary(frequencyPair, uniquingKeysWith: +)
}
Usage : print(characterFrequencies(of: "Happy"))
Result : ["a": 1, "H": 1, "y": 1, "p": 2]
func repeatedCharaterPrint(inputArray: [String]) -> [String:Int] {
var dict = [String:Int]()
if inputArray.count > 0 {
for char in inputArray {
if let keyExists = dict[char], keyExists != nil {
dict[char] = Int(dict[char] ?? 0) + 1
}else {
dict[char] = 1
}
}
}
return dict
}
let aa = ["a","s","f","s","l","s"]
print(repeatedCharaterPrint(inputArray: aa))
//Answer : "["s": 3, "l": 1, "a": 1, "f": 1]"
This solution is written using hash function , so computation time would be O(1). good for long strings.
//Extension On String and Characters to get Ascii values and Char from Ascii
extension Character {
//Get Ascii Value of Char
var asciiValue:UInt32? {
return String(self).unicodeScalars.filter{$0.isASCII}.first?.value
}
}
extension String {
//Char Char from Ascii Value
init(unicodeScalar: UnicodeScalar) {
self.init(Character(unicodeScalar))
}
init?(unicodeCodepoint: Int) {
if let unicodeScalar = UnicodeScalar(unicodeCodepoint) {
self.init(unicodeScalar: unicodeScalar)
} else {
return nil
}
}
static func +(lhs: String, rhs: Int) -> String {
return lhs + String(unicodeCodepoint: rhs)!
}
static func +=(lhs: inout String, rhs: Int) {
lhs = lhs + rhs
}
}
extension String {
///Get Char at Index from String
var length: Int {
return self.characters.count
}
subscript (i: Int) -> String {
return self[Range(i ..< i + 1)]
}
func substring(from: Int) -> String {
return self[Range(min(from, length) ..< length)]
}
func substring(to: Int) -> String {
return self[Range(0 ..< max(0, to))]
}
subscript (r: Range<Int>) -> String {
let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)),
upper: min(length, max(0, r.upperBound))))
let start = index(startIndex, offsetBy: range.lowerBound)
let end = index(start, offsetBy: range.upperBound - range.lowerBound)
return self[Range(start ..< end)]
}
}
//Program :
let strk = "aacncjkvkevkklvkdsjkbvjsdbvjkbsdjkvbjdsbvjkbsvbkjwlnkneilhfleknkeiohlgblehgilkbskdbvjdsbvjkdsbvbbvsbdvjlbsdvjbvjkdbvbsjdbjsbvjbdjbjbjkbjkvbjkbdvjbdjkvbjdbvjdbvjbvjdsbjkvbdsjvbkjsbvadvbjkenevknkenvnekvjksbdjvbjkbjbvbkjvbjdsbvjkbdskjvbdsbvjkdsbkvbsdkjbvkjsbvjsbdjkvbdsbvjkbdsvjbdefghaj"
print(strk)
//Declare array of fixes size 26 (characters) or you can say it as a hash table
var freq = [Int](repeatElement(0, count: 26))
func hashFunc(char : Character) -> UInt32 {
guard let ascii = char.asciiValue else {
return 0
}
return ascii - 97 //97 used for ascii value of a
}
func countFre(string:String) {
for i in 0 ... string.characters.count-1 {
let charAtIndex = string[i].characters.first!
let index = hashFunc(char: charAtIndex)
let currentVal = freq[Int(index)]
freq[Int(index)] = currentVal + 1
//print("CurrentVal of \(charAtIndex) with index \(index) is \(currentVal)")
}
for charIndex in 0 ..< 26 {
print(String(unicodeCodepoint: charIndex+97)!,freq[charIndex])
}
}
countFre(string: strk)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do I check if my string only contains [0-9] and .?
I’d like all other symbols to be deleted from my string.
If a user inputs any String
var anyString:String = “3f00b6r.beAwes0me4me”
I want to extract all the numbers from it (including one . if present) and remove all the other characters.
var myDouble:Double = 3006.04
Someone who know how to do that ?
You can use NSRegularExpression, like this:
// First set up error for checking
var error:NSError?
// Create regular expression pattern to search for numbers one digit in length or longer
let regexString = NSRegularExpression(pattern: "[0-9]{1,}.[0-9]{1,}|[0-9]{1,}", options: nil, error: &error)
// example string
let string = "find the number 10.19"
// get the string length
let strLength = count(string)
// To return the result first of all obtain the range of the first match if there is one, if let used here because NSRegularExpression returns an optional (we could of checked earlier, but I didn't)
if let rangeOfFirstMatch = regexString?.rangeOfFirstMatchInString(string, options: nil, range: NSMakeRange(0, strLength))
{
// if a range has been found that isn't equal to NSNotFound then build a range for the found string
if !NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)) {
advance(string.startIndex, rangeOfFirstMatch.length)
let r = Range(start: advance(string.startIndex, rangeOfFirstMatch.location), end: advance(string.startIndex, rangeOfFirstMatch.location+rangeOfFirstMatch.length))
// return the value
let substringForFirstMatch = string.substringWithRange(r)
println("Here's your match: \(substringForFirstMatch)")
}
}
There's also String.stringByTrimmingCharactersInSet() which would be cleaner and quicker but isn't as versatile. For instance the above string would also be returned if you did this:
let set = NSMutableCharacterSet.alphanumericCharacterSet()
set.addCharactersInString(" ")
set.removeCharactersInString("0123456789.")
string.stringByTrimmingCharactersInSet(set)
But if the string changed to "find the number ??...10.19...." then the stringByTrimmingCharactersInSet() would let you down and return "??...10.19...." while the regular expression version would hold up.
To make things easier a String extension could be written like this for returning multiple numbers in a string of type Double or Int:
extension String {
func stringsMatchingRegularExpression(expression exp:String, error err:NSErrorPointer) -> [String]? {
var strArray:[String]?
var rangeArray:[NSRange]?
let strLength = count(self)
var startOfRange = 0
if let regexString = NSRegularExpression(pattern: exp, options: nil, error: err) {
while startOfRange <= strLength {
let rangeOfMatch = regexString.rangeOfFirstMatchInString(self, options: nil, range: NSMakeRange(startOfRange, strLength-startOfRange))
if let rArray = rangeArray {
rangeArray = rArray + [rangeOfMatch]
}
else {
rangeArray = [rangeOfMatch]
}
startOfRange = rangeOfMatch.location+rangeOfMatch.length
}
if let ranArr = rangeArray {
for r in ranArr {
if !NSEqualRanges(r, NSMakeRange(NSNotFound, 0)) {
advance(self.startIndex, r.length)
let r = Range(start: advance(self.startIndex, r.location), end: advance(string.startIndex, r.location+r.length))
// return the value
let substringForMatch = self.substringWithRange(r)
if let sArray = strArray {
strArray = sArray + [substringForMatch]
}
else {
strArray = [substringForMatch]
}
}
}
}
}
return strArray
}
}
let myString = "one number is 7.5, another is 20"
let subStringArray = myString.stringsMatchingRegularExpression(expression: "[0-9]{1,}.[0-9]{1,}|[0-9]{1,}", error: nil)
subStringArray?[0] // 7.5
subStringArray?[1] // 20
Swift 2.0 Update for Extension
extension String {
func stringsMatchingRegularExpression(expression exp:String) -> [String]? {
var strArray:[String]?
var rangeArray:[NSRange]?
let strLength = self.characters.count
var startOfRange = 0
do {
let regexString = try NSRegularExpression(pattern: exp, options: [])
while startOfRange <= strLength {
let rangeOfMatch = regexString.rangeOfFirstMatchInString(self, options: [], range: NSMakeRange(startOfRange, strLength-startOfRange))
if let rArray = rangeArray {
rangeArray = rArray + [rangeOfMatch]
}
else {
rangeArray = [rangeOfMatch]
}
startOfRange = rangeOfMatch.location+rangeOfMatch.length
}
if let ranArr = rangeArray {
for r in ranArr {
if !NSEqualRanges(r, NSMakeRange(NSNotFound, 0)) {
self.startIndex.advancedBy(r.length)
let r = Range(start: self.startIndex.advancedBy(r.location), end: self.startIndex.advancedBy(r.location + r.length))
// return the value
let substringForMatch = self.substringWithRange(r)
if let sArray = strArray {
strArray = sArray + [substringForMatch]
}
else {
strArray = [substringForMatch]
}
}
}
}
} catch {
}
return strArray
}
}
let myString = "one number is 7.5, another is 20"
let subStringArray = myString.stringsMatchingRegularExpression(expression: "[-+]?\\d+.?\\d+")
subStringArray?[0] // 7.5
subStringArray?[1] // 20
Swift 3.0 Update for Extension
extension String {
func stringsMatchingRegularExpression(expression exp:String) -> [String]? {
var strArray:[String]?
var rangeArray:[NSRange]?
let strLength = self.characters.count
var startOfRange = 0
do {
let regexString = try NSRegularExpression(pattern: exp, options: [])
while startOfRange <= strLength {
let rangeOfMatch = regexString.rangeOfFirstMatch(in: self, options: [], range: NSMakeRange(startOfRange, strLength-startOfRange))
if let rArray = rangeArray {
rangeArray = rArray + [rangeOfMatch]
}
else {
rangeArray = [rangeOfMatch]
}
startOfRange = rangeOfMatch.location+rangeOfMatch.length
}
if let ranArr = rangeArray {
for r in ranArr {
if !NSEqualRanges(r, NSMakeRange(NSNotFound, 0)) {
self.index(startIndex, offsetBy: r.length)
let r = self.index(startIndex, offsetBy:r.location)..<self.index(startIndex, offsetBy:r.location + r.length)
// return the value
let substringForMatch = self.substring(with: r)
if let sArray = strArray {
strArray = sArray + [substringForMatch]
}
else {
strArray = [substringForMatch]
}
}
}
}
}
catch {
// catch errors here
}
return strArray
}
}
let myString = "one number is 7.5, another is 20"
let subStringArray = myString.stringsMatchingRegularExpression(expression: "[-+]?\\d+.?\\d+")
subStringArray?[0] // 7.5
subStringArray?[1] // 20