Skip to content

Commit

Permalink
Merge pull request pNre#124 from skywinder/regex-contains-match
Browse files Browse the repository at this point in the history
Add containsMatch & replaceMatches methods
  • Loading branch information
pNre committed Jun 8, 2015
2 parents b3482cc + f5460a1 commit 864c6a2
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 102 deletions.
33 changes: 33 additions & 0 deletions ExSwift/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@ public extension String {
return nil
}

/**
Check is string with this pattern included in string

:param: pattern Pattern to match
:param: ignoreCase true for case insensitive matching
:returns: true if contains match, otherwise false
*/
func containsMatch (pattern: String, ignoreCase: Bool = false) -> Bool? {
if let regex = ExSwift.regex(pattern, ignoreCase: ignoreCase) {
let range = NSMakeRange(0, count(self))
return regex.firstMatchInString(self, options: .allZeros, range: range) != nil
}

return nil
}

/**
Replace all pattern matches with another string

:param: pattern Pattern to match
:param: replacementString string to replace matches
:param: ignoreCase true for case insensitive matching
:returns: true if contains match, otherwise false
*/
func replaceMatches (pattern: String, withString replacementString: String, ignoreCase: Bool = false) -> String? {
if let regex = ExSwift.regex(pattern, ignoreCase: ignoreCase) {
let range = NSMakeRange(0, count(self))
return regex.stringByReplacingMatchesInString(self, options: .allZeros, range: range, withTemplate: replacementString)
}

return nil
}

/**
Inserts a substring at the given index in self.

Expand Down
Loading

0 comments on commit 864c6a2

Please sign in to comment.