Created
July 18, 2022 13:33
-
-
Save jayesh15111988/7bde41f0043ac9fdcb51dbfff367c9b9 to your computer and use it in GitHub Desktop.
The Gist to summarize how to use Regular expression on iOS platform using Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
findMatches() | |
findMatchingElements() | |
replaceMatchingElements() | |
} | |
func findMatches() { | |
let regex = "\\W+offset=\\d+&limit=\\d+" | |
let inputText = "https://pokeapi.co/api/v2/pokemon-species?offset=2&limit=2" | |
let result = doesMatchExist(regularExpression: regex, inputText: inputText) | |
print(result) | |
} | |
func findMatchingElements() { | |
var regex = "(#\\/{0,1}\\d{1,}#\\*{0,2})" | |
var inputText = "We are big now #1#**lot of sales#/1#* the money and cards #2#Robert Langdon and Ambra Vidal#/2#**." | |
let allMatchesSimple = getMatches(regex: regex, inputText: inputText) | |
print(allMatchesSimple) | |
regex = "\\W+offset=(\\d+)&limit=(\\d+)" | |
inputText = "https://pokeapi.co/api/v2/pokemon-species?offset=2&limit=2" | |
let allMatchesComplex = getMatches(regex: regex, inputText: inputText) | |
print(allMatchesComplex) | |
} | |
func replaceMatchingElements() { | |
let regex = "(#\\/{0,1}\\d{1,}#\\*{0,2})" | |
let inputText = "We are big now #1#**lot of sales#/1#* the money and cards #2#Robert Langdon and Ambra Vidal#/2#**." | |
let updatedInputText = stringAfterReplacingMatches(regex: regex, inputText: inputText) { originalMatchingGroup in | |
return "" | |
} | |
print(updatedInputText) | |
let regex1 = "\\W+offset=(\\d+)&limit=(\\d+)" | |
let inputText1 = "?offset=400&limit=20" | |
let anotherUpdatedInputText = stringAfterReplacingMatches(regex: regex1, inputText: inputText1) { originalMatchingGroup in | |
return "Awesome" + originalMatchingGroup | |
} | |
print(anotherUpdatedInputText) | |
} | |
func doesMatchExist(regularExpression: String, inputText: String) -> Bool { | |
guard let regex = try? NSRegularExpression(pattern: regularExpression) else { | |
return false | |
} | |
return regex.firstMatch(in: inputText, range: NSRange(inputText.startIndex..., in: inputText)) != nil | |
} | |
func getMatches(regex: String, inputText: String) -> [String] { | |
guard let regex = try? NSRegularExpression(pattern: regex) else { | |
return [] | |
} | |
let results = regex.matches(in: inputText, | |
range: NSRange(inputText.startIndex..., in: inputText)) | |
let finalResult = results.map { match in | |
return (0..<match.numberOfRanges).map { range -> String in | |
let rangeBounds = match.range(at: range) | |
guard let range = Range(rangeBounds, in: inputText) else { | |
return "" | |
} | |
return String(inputText[range]) | |
} | |
}.filter { !$0.isEmpty } | |
var allMatches: [String] = [] | |
// Iterate over the final result which includes all the matches and groups | |
// We will store all the matching strings | |
for result in finalResult { | |
for (index, resultText) in result.enumerated() { | |
// Skip the match. Go to the next elements which represent matching groups | |
if index == 0 { | |
continue | |
} | |
allMatches.append(resultText) | |
} | |
} | |
return allMatches | |
} | |
func stringAfterReplacingMatches(regex: String, inputText: String, replacementStringClosure: (String) -> String?) -> String { | |
guard let regex = try? NSRegularExpression(pattern: regex) else { | |
return inputText | |
} | |
let results = regex.matches(in: inputText, range: NSRange(inputText.startIndex..., in: inputText)) | |
var outputText = inputText | |
results.reversed().forEach { match in | |
(1..<match.numberOfRanges).reversed().forEach { rangeIndex in | |
let matchingGroup: String = (inputText as NSString).substring(with: match.range(at: rangeIndex)) | |
let rangeBounds = match.range(at: rangeIndex) | |
guard let range = Range(rangeBounds, in: inputText) else { | |
return | |
} | |
let replacement = replacementStringClosure(matchingGroup) ?? matchingGroup | |
outputText = outputText.replacingOccurrences(of: matchingGroup, with: replacement, range: range) | |
} | |
} | |
return outputText | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment