-
Notifications
You must be signed in to change notification settings - Fork 13
Regular expressions in Swift #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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) | ||
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. If you are not stopping an execution on |
||
} 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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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".