Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose you to omit | ||
let results = regex.matches(in: testString, options: [], range: NSMakeRange(0, testString.characters.count)) // Matches | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must use testString.utf16.count here. Because NSRegularExpression works with NSString, which are utf16 encoded. | ||
let result = results.map { nsString.substring(with: $0.range) } | ||
print(result) | ||
} catch let error as NSError { // Handling exception | ||
print("Invalid regex: \(error.localizedDescription)") | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I supposed "Cating" should be "Casting". And maybe not "special" but "specific".