Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 864c6a2

Browse files
committed
Merge pull request pNre#124 from skywinder/regex-contains-match
Add containsMatch & replaceMatches methods
2 parents b3482cc + f5460a1 commit 864c6a2

File tree

2 files changed

+165
-102
lines changed

2 files changed

+165
-102
lines changed

‎ExSwift/String.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ public extension String {
113113
return nil
114114
}
115115

116+
/**
117+
Check is string with this pattern included in string
118+
119+
:param: pattern Pattern to match
120+
:param: ignoreCase true for case insensitive matching
121+
:returns: true if contains match, otherwise false
122+
*/
123+
func containsMatch (pattern: String, ignoreCase: Bool = false) -> Bool? {
124+
if let regex = ExSwift.regex(pattern, ignoreCase: ignoreCase) {
125+
let range = NSMakeRange(0, count(self))
126+
return regex.firstMatchInString(self, options: .allZeros, range: range) != nil
127+
}
128+
129+
return nil
130+
}
131+
132+
/**
133+
Replace all pattern matches with another string
134+
135+
:param: pattern Pattern to match
136+
:param: replacementString string to replace matches
137+
:param: ignoreCase true for case insensitive matching
138+
:returns: true if contains match, otherwise false
139+
*/
140+
func replaceMatches (pattern: String, withString replacementString: String, ignoreCase: Bool = false) -> String? {
141+
if let regex = ExSwift.regex(pattern, ignoreCase: ignoreCase) {
142+
let range = NSMakeRange(0, count(self))
143+
return regex.stringByReplacingMatchesInString(self, options: .allZeros, range: range, withTemplate: replacementString)
144+
}
145+
146+
return nil
147+
}
148+
116149
/**
117150
Inserts a substring at the given index in self.
118151

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /