diff --git a/Swift/match.playground/Contents.swift b/Swift/match.playground/Contents.swift new file mode 100644 index 0000000..eabfcdc --- /dev/null +++ b/Swift/match.playground/Contents.swift @@ -0,0 +1,19 @@ +import Foundation + +let testString = "one 1 two 2 three 3" + +let pattern = "[0-9]" // Matching numbers +let nsString = testString as NSString // Cating to NSString to use special methods + +// Trying to initialize regex +do { + let regex = try NSRegularExpression(pattern: pattern, options: []) + let results = regex.matches(in: testString, options: [], range: NSMakeRange(0, testString.characters.count)) // Matches + let result = results.map { nsString.substring(with: $0.range) } + print(result) +} catch let error as NSError { // Handling exception + print("Invalid regex: \(error.localizedDescription)") +} + + + diff --git a/Swift/match.playground/contents.xcplayground b/Swift/match.playground/contents.xcplayground new file mode 100644 index 0000000..63b6dd8 --- /dev/null +++ b/Swift/match.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Swift/match.playground/playground.xcworkspace/contents.xcworkspacedata b/Swift/match.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Swift/match.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Swift/match.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate b/Swift/match.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..d92a9a6 Binary files /dev/null and b/Swift/match.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Swift/replace.playground/Contents.swift b/Swift/replace.playground/Contents.swift new file mode 100644 index 0000000..8c8becb --- /dev/null +++ b/Swift/replace.playground/Contents.swift @@ -0,0 +1,17 @@ +import Foundation + +let helloString = "Hello, World!" // Defining initial string + +// Initializing regular expression +var replaceRegExp: NSRegularExpression? + +do { + replaceRegExp = try NSRegularExpression(pattern: "World", options: .allowCommentsAndWhitespace) +} catch let error as NSError { // Handling exception + print(error.localizedDescription) +} + +// Initializing new string with substring, replaced by template +let newString = replaceRegExp?.stringByReplacingMatches(in: helloString, options: .withTransparentBounds, range: NSRange(location: 0, length: helloString.characters.count), withTemplate: "Kitten") + +print(newString ?? "Replacing failed", "\n") \ No newline at end of file diff --git a/Swift/replace.playground/contents.xcplayground b/Swift/replace.playground/contents.xcplayground new file mode 100644 index 0000000..63b6dd8 --- /dev/null +++ b/Swift/replace.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Swift/replace.playground/playground.xcworkspace/contents.xcworkspacedata b/Swift/replace.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Swift/replace.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Swift/replace.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate b/Swift/replace.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..3c64c85 Binary files /dev/null and b/Swift/replace.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Swift/split.playground/Contents.swift b/Swift/split.playground/Contents.swift new file mode 100644 index 0000000..54f1445 --- /dev/null +++ b/Swift/split.playground/Contents.swift @@ -0,0 +1,19 @@ +import Foundation + +let initialString = "This-is-string-to-split-by-special-character" // Defining initial string + +let splitter = "-" // Defining splitter +let stop = "//" + +// Initializing regular expression +var splitRegExp: NSRegularExpression? +do { + splitRegExp = try NSRegularExpression(pattern: splitter, options: .allowCommentsAndWhitespace) +} catch let error as NSError { // Handling exception + print(error.localizedDescription) +} + +let modifiedString = splitRegExp?.stringByReplacingMatches(in: initialString, options: .withTransparentBounds, range: NSRange(location: 0, length: initialString.characters.count), withTemplate: stop) + +// But in Swift this method can be used +print(modifiedString?.components(separatedBy: stop) ?? "Splitting failed", "\n") \ No newline at end of file diff --git a/Swift/split.playground/contents.xcplayground b/Swift/split.playground/contents.xcplayground new file mode 100644 index 0000000..63b6dd8 --- /dev/null +++ b/Swift/split.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Swift/split.playground/playground.xcworkspace/contents.xcworkspacedata b/Swift/split.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Swift/split.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Swift/split.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate b/Swift/split.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..d750078 Binary files /dev/null and b/Swift/split.playground/playground.xcworkspace/xcuserdata/anastasia.xcuserdatad/UserInterfaceState.xcuserstate differ