Open
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Original file line numberDiff line numberDiff 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

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".


// Trying to initialize regex
do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
Copy link

@Gagnant Gagnant Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose you to omit options parameter because it has default value []

let results = regex.matches(in: testString, options: [], range: NSMakeRange(0, testString.characters.count)) // Matches

Choose a reason for hiding this comment

The 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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)
Copy link

@GreatAndPowerfulKing GreatAndPowerfulKing Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are not stopping an execution on NSRegularExpression initialization failure, you may replace replaceRegExp declaration with: let 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 numberDiff line numberDiff 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>
Binary file not shown.
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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>
Binary file not shown.