Regex to find and replace digits - ios

I am new to regular expressions and need to perform a specific task with the same. I would require a regex that performs global search and checks if there are 3 or more consecutive digits in a string and if yes, then replaces all the digits with "xxxx".
For example, the string
abcdef 12 quews 4567
should be changed to
abcdef XX quews XXXX
Any help would be appreciated. Thanks.

UPDATED
Use this
func testingRegex6(text:String) ->String{
do{
var finalText = text
let componentNSString = NSString.init(string:text)
let regex = try NSRegularExpression(pattern: "\\d+", options:[.dotMatchesLineSeparators])
let matches = regex.matches(in: text ,options: [], range: NSMakeRange(0, componentNSString.length)).reversed()
if(matches.filter({$0.range.length >= 3}).count > 0) { //here we check if we have any substring containing 3 o more digits
for result3 in matches {
finalText = finalText.replacingOccurrences(of: componentNSString.substring(with: result3.range), with: Array(repeating: "X", count: result3.range.length).joined())
}
}
return finalText
}
catch{
return ""
}
return ""
}
Input "abcdef 12 quews 4567" Log "abcdef XX quews XXXX"
Input "abcdef 12 que82ws 45" Log "abcdef 12 que82ws 45"

With Reg. expression
let txt = "abc 12 wqer 987asd asdf1233sadf"
do {
let regx = try NSRegularExpression(pattern: "[0-9]", options: .caseInsensitive)
let result = regx.stringByReplacingMatches(in: txt, options: .withTransparentBounds, range: NSMakeRange(0, txt.count), withTemplate: "X")
print(result)
} catch {
print(error)
}
Without reg. expression
let txt = "abc 12 wqer 987asd asdf1233sadf"
let replacedStr = String(txt.map {
let c = String($0)
if let digit = Int(c), digit >= 0, digit <= 9 {
return "X"
} else {
return $0
}
})
print(replacedStr)

let str = "this is 123 my vie22w"
Convert string into string array
let characters = Array(str)
Use loop with counter and replace string if digits are more than 3.
var count = 0
for i in characters {
if i >= "0" && i <= "9" {
count += 1
}
}
if count > 3 {
let output = str.replacingOccurrences(of: "[\\[\\]^[0-9]]", with: "X", options: .regularExpression, range: nil)
print(output)
}

Use this,
let testString = "abcdef 12 quews 4567"
let output = testString.replacingOccurrences(of: "[\\[\\]^[0-9]]", with: "X", options: .regularExpression, range: nil)
print(output)
Your output looks like this,
abcdef XX quews XXXX
Update
Find String extension class,
extension String {
func matches(for regex: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: self, range: NSRange(self.startIndex..., in: self))
return results.map {
String(self[Range($0.range, in: self)!])
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
}
and find below code,
let temp = "abc44def 12 quews 4564 1254"
let array = temp.matches(for: "\\d{3,}")
var output = temp
for val in array.enumerated() {
var string = ""
for _ in 0..<val.element.count {
string+="X"
}
output = output.replacingOccurrences(of: val.element, with: string)
}
print(output)
you will get output like this,
abc44def 12 quews XXXX XXXX

Thank you for all your answers, with all your inputs I was able to solve this with following code
extension String {
func replaceDigits() -> String {
do {
let regex = try NSRegularExpression(pattern: "\\d{3,}", options: [])
let matches = regex.numberOfMatches(in: self, options: [], range: NSRange(location: 0, length: self.count))
if matches > 0 {
return replacingOccurrences(of: "\\d", with: "X", options: .regularExpression, range: nil)
}
} catch {
return self
}
return self
}
}

Related

case insensitive splitting swift 4?

Trying to split values in swift, and it works, but it is cAsE sEnSiTiVe. Is there a way of splitting them in swift 4 ignoring case?
"Hello There!"
"hello there!"
I am currently using String.components(seperatedBy: "Th") but that does only split the second string, "hello there!". Is there a way to split them both?
You can write something like this:
import Foundation
let str1 = "Hello There!"
let str2 = "hello there!"
extension String {
func caseInsensitiveSplit(separator: String) -> [String] {
//Thanks for Carpsen90. Please see comments below.
if separator.isEmpty {
return [self] //generates the same output as `.components(separatedBy: "")`
}
let pattern = NSRegularExpression.escapedPattern(for: separator)
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let matches = regex.matches(in: self, options: [], range: NSRange(0..<self.utf16.count))
let ranges = (0..<matches.count+1).map { (i: Int)->NSRange in
let start = i == 0 ? 0 : matches[i-1].range.location + matches[i-1].range.length
let end = i == matches.count ? self.utf16.count: matches[i].range.location
return NSRange(location: start, length: end-start)
}
return ranges.map {String(self[Range($0, in: self)!])}
}
}
print( str1.caseInsensitiveSplit(separator: "th") ) //->["Hello ", "ere!"]
print( str2.caseInsensitiveSplit(separator: "th") ) //->["hello ", "ere!"]
But I wonder what you want to do with "hello " and "ere!".
(You lose case-info of the separator, if it matched with "th", "tH", "Th" or "TH".)
If you can explain what you really want to do, someone would show you a better solution for it.
Just for fun another version using NSRegularExpression but extending StringProtocol:
Swift 4 or later
extension StringProtocol {
func caseInsensitiveComponents<T>(separatedBy separator: T) -> [SubSequence] where T: StringProtocol, Index == String.Index {
var index = startIndex
return ((try? NSRegularExpression(pattern: NSRegularExpression.escapedPattern(for: String(separator)), options: .caseInsensitive))?.matches(in: String(self), range: NSRange(location: 0, length: utf16.count))
.compactMap {
guard let range = Range($0.range, in: String(self))
else { return nil }
defer { index = range.upperBound }
return self[index..<range.lowerBound]
} ?? []) + [self[index...]]
}
}
Playground testing
let str1 = "Hello There!"
let str2 = "hello there!"
str1.caseInsensitiveComponents(separatedBy: "Th") // ["Hello ", "ere!"]
str2.caseInsensitiveComponents(separatedBy: "Th") // ["Hello ", "ere!"]
An possible solution is to get the range of the separator with .caseInsensitive option and extract the substrings from its lower- and upperBound.
extension StringProtocol where Index == String.Index {
func caseInsensitiveComponents(separatedBy separator: String) -> [SubSequence]
{
var result = [SubSequence]()
var currentIndex = startIndex
while let separatorRange = self[currentIndex...].range(of: separator, options: .caseInsensitive) {
result.append(self[currentIndex..<separatorRange.lowerBound])
currentIndex = separatorRange.upperBound
}
return result + [self[currentIndex...]]
}
}
You could lower case the whole string first:
let s = "Hello There!".lowercased().components(separatedBy: "th")
Not sure if i understood what you are trying to achieve, but you can try this.
var str = "Hello There!"
var str2 = "hello there!"
override func viewDidLoad() {
super.viewDidLoad()
let split1 = str.lowercased().replacingOccurrences(of: "th", with: " ")
let split2 = str2.lowercased().replacingOccurrences(of: "th", with: " ")
print(split1) //hello ere!
print(split2) //hello ere!
}
If you want to filter for a list of items containing a certain keyword, split the strings is the wrong approach. All you need is a simple check whether that keyword appears in the item:
let queryStr = "th"
let items = [
"Hello There!",
"hello there!"
]
let filterdItems = items.filter {
return $0.range(of: queryStr, options: .caseInsensitive) != nil
}

Separating CamelCase string into space-separated words in Swift

I would like to separate a CamelCase string into space-separated words in a new string. Here is what I have so far:
var camelCaps: String {
guard self.count > 0 else { return self }
var newString: String = ""
let uppercase = CharacterSet.uppercaseLetters
let first = self.unicodeScalars.first!
newString.append(Character(first))
for scalar in self.unicodeScalars.dropFirst() {
if uppercase.contains(scalar) {
newString.append(" ")
}
let character = Character(scalar)
newString.append(character)
}
return newString
}
let aCamelCaps = "aCamelCaps"
let camelCapped = aCamelCaps.camelCaps // Produce: "a Camel Caps"
let anotherCamelCaps = "ÄnotherCamelCaps"
let anotherCamelCapped = anotherCamelCaps.camelCaps // "Änother Camel Caps"
I'm inclined to suspect that this may not be the most efficient way to convert to space-separated words, if I call it in a tight loop, or 1000's of times. Are there more efficient ways to do this in Swift?
[Edit 1:] The solution I require should remain general for Unicode scalars, not specific to Roman ASCII "A..Z".
[Edit 2:] The solution should also skip the first letter, i.e. not prepend a space before the first letter.
[Edit 3:] Updated for Swift 4 syntax, and added caching of uppercaseLetters, which improves performance in very long strings and tight loops.
extension String {
func camelCaseToWords() -> String {
return unicodeScalars.dropFirst().reduce(String(prefix(1))) {
return CharacterSet.uppercaseLetters.contains($1)
? $0 + " " + String($1)
: $0 + String($1)
}
}
}
print("ÄnotherCamelCaps".camelCaseToWords()) // Änother Camel Caps
May be helpful for someone :)
One Line Solution
I concur with #aircraft, regular expressions can solve this problem in one LOC!
// Swift 5 (and probably 4?)
extension String {
func titleCase() -> String {
return self
.replacingOccurrences(of: "([A-Z])",
with: " $1",
options: .regularExpression,
range: range(of: self))
.trimmingCharacters(in: .whitespacesAndNewlines)
.capitalized // If input is in llamaCase
}
}
Props to this JS answer.
P.S. I have a gist for snake_case → CamelCase here.
P.P.S. I updated this for New Swift (currently 5.1), then saw #busta's answer, and swapped out my startIndex..<endIndex for his range(of: self). Credit where it's due y'all!
a better full swifty solution... based on AmitaiB answer
extension String {
func titlecased() -> String {
return self.replacingOccurrences(of: "([A-Z])", with: " $1", options: .regularExpression, range: self.range(of: self))
.trimmingCharacters(in: .whitespacesAndNewlines)
.capitalized
}
}
I might be late but I want to share a little improvement to Augustine P A answer or Leo Dabus comment.
Basically, that code won't work properly if we are using upper camel case notation (like "DuckDuckGo") because it will add a space at the beginning of the string.
To address this issue, this is a slightly modified version of the code, using Swift 3.x, and it's compatible with both upper and lower came case:
extension String {
func camelCaseToWords() -> String {
return unicodeScalars.reduce("") {
if CharacterSet.uppercaseLetters.contains($1) {
if $0.count > 0 {
return ($0 + " " + String($1))
}
}
return $0 + String($1)
}
}
}
As far as I tested on my old MacBook, your code seems to be efficient enough for short strings:
import Foundation
extension String {
var camelCaps: String {
var newString: String = ""
let upperCase = CharacterSet.uppercaseLetters
for scalar in self.unicodeScalars {
if upperCase.contains(scalar) {
newString.append(" ")
}
let character = Character(scalar)
newString.append(character)
}
return newString
}
var camelCaps2: String {
var newString: String = ""
let upperCase = CharacterSet.uppercaseLetters
var range = self.startIndex..<self.endIndex
while let foundRange = self.rangeOfCharacter(from: upperCase,range: range) {
newString += self.substring(with: range.lowerBound..<foundRange.lowerBound)
newString += " "
newString += self.substring(with: foundRange)
range = foundRange.upperBound..<self.endIndex
}
newString += self.substring(with: range)
return newString
}
var camelCaps3: String {
struct My {
static let regex = try! NSRegularExpression(pattern: "[A-Z]")
}
return My.regex.stringByReplacingMatches(in: self, range: NSRange(0..<self.utf16.count), withTemplate: " $0")
}
}
let aCamelCaps = "aCamelCaps"
assert(aCamelCaps.camelCaps == aCamelCaps.camelCaps2)
assert(aCamelCaps.camelCaps == aCamelCaps.camelCaps3)
let t0 = Date().timeIntervalSinceReferenceDate
for _ in 0..<1_000_000 {
let aCamelCaps = "aCamelCaps"
let camelCapped = aCamelCaps.camelCaps
}
let t1 = Date().timeIntervalSinceReferenceDate
print(t1-t0) //->4.78703999519348
for _ in 0..<1_000_000 {
let aCamelCaps = "aCamelCaps"
let camelCapped = aCamelCaps.camelCaps2
}
let t2 = Date().timeIntervalSinceReferenceDate
print(t2-t1) //->10.5831440091133
for _ in 0..<1_000_000 {
let aCamelCaps = "aCamelCaps"
let camelCapped = aCamelCaps.camelCaps3
}
let t3 = Date().timeIntervalSinceReferenceDate
print(t3-t2) //->14.2085000276566
(Do not try to test the code above in the Playground. The numbers are taken from a single trial executed as a CommandLine app.)
extension String {
func titlecased() -> String {
return self
.replacingOccurrences(of: "([a-z])([A-Z](?=[A-Z])[a-z]*)", with: "$1 $2", options: .regularExpression)
.replacingOccurrences(of: "([A-Z])([A-Z][a-z])", with: "$1 $2", options: .regularExpression)
.replacingOccurrences(of: "([a-z])([A-Z][a-z])", with: "$1 $2", options: .regularExpression)
.replacingOccurrences(of: "([a-z])([A-Z][a-z])", with: "$1 $2", options: .regularExpression)
}
}
In
"ThisStringHasNoSpacesButItDoesHaveCapitals"
"IAmNotAGoat"
"LOLThatsHilarious!"
"ThisIsASMSMessage"
Out
"This String Has No Spaces But It Does Have Capitals"
"I Am Not A Goat"
"LOL Thats Hilarious!"
"This Is ASMS Message" // (Difficult tohandle single letter words when they are next to acronyms.)
enter link description here
I can do this extension in less lines of code (and without a CharacterSet), but yes, you basically have to enumerate each String if you want to insert spaces in front of capital letters.
extension String {
var differentCamelCaps: String {
var newString: String = ""
for eachCharacter in self {
if "A"..."Z" ~= eachCharacter {
newString.append(" ")
}
newString.append(eachCharacter)
}
return newString
}
}
print("ÄnotherCamelCaps".differentCamelCaps) // Änother Camel Caps
Swift 5 solution
extension String {
func camelCaseToWords() -> String {
return unicodeScalars.reduce("") {
if CharacterSet.uppercaseLetters.contains($1) {
if $0.count > 0 {
return ($0 + " " + String($1))
}
}
return $0 + String($1)
}
}
}
If you want to make it more efficient, you can use Regular Expressions.
extension String {
func replace(regex: NSRegularExpression, with replacer: (_ match:String)->String) -> String {
let str = self as NSString
let ret = str.mutableCopy() as! NSMutableString
let matches = regex.matches(in: str as String, options: [], range: NSMakeRange(0, str.length))
for match in matches.reversed() {
let original = str.substring(with: match.range)
let replacement = replacer(original)
ret.replaceCharacters(in: match.range, with: replacement)
}
return ret as String
}
}
let camelCaps = "aCamelCaps" // there are 3 Capital character
let pattern = "[A-Z]"
let regular = try!NSRegularExpression(pattern: pattern)
let camelCapped:String = camelCaps.replace(regex: regular) { " \($0)" }
print("Uppercase characters replaced: \(camelCapped)")
Here's what I came up with using Unicode character classes: (Swift 5)
extension String {
var titleCased: String {
self
.replacingOccurrences(of: "(\\p{UppercaseLetter}\\p{LowercaseLetter}|\\p{UppercaseLetter}+(?=\\p{UppercaseLetter}))",
with: " $1",
options: .regularExpression,
range: range(of: self)
)
.capitalized
}
}
Output:
fillPath ➝ Fill Path
ThisStringHasNoSpaces ➝ This String Has No Spaces
IAmNotAGoat ➝ I Am Not A Goat
LOLThatsHilarious! ➝ Lol Thats Hilarious!
ThisIsASMSMessage ➝ This Is Asms Message
Swift way:
extension String {
var titlecased: String {
map { ($0.isUppercase ? " " : "") + String($0) }
.joined(separator: "")
.trimmingCharacters(in: .whitespaces)
}
}
Swift 5+
Small style improvements on previous answers
import Foundation
extension String {
func camelCaseToWords() -> String {
unicodeScalars.reduce("") {
guard CharacterSet.uppercaseLetters.contains($1),
$0.count > 0
else { return $0 + String($1) }
return ($0 + " " + String($1))
}
}
}
Using guard let statements is usually recommended, as they provide an "early exit" for non matching cases and decrease the overall nesting levels of your code (which usually improves readability quite a lot... and remember, readability counts!)
Solution with REGEX
let camelCase = "SomeATMInTheShop"
let regexPattern = "[A-Z-_&](?=[a-z0-9]+)|[A-Z-_&]+(?![a-z0-9])"
let newValue = camelCase.replacingOccurrences(of: regexPattern, with: " $0", options: .regularExpression, range: nil)
Otuput ==> Some ATM In The Shop

Extract 5 digit zip code from string

I'm letting a user enter their address and I need to extract the zip code from it.
I found that this RegEx should work: \d{5}([ \-]\d{4})? however I'm having an extremely difficult time getting this to work on Swift.
This is where I"m at:
private func sanatizeZipCodeString() -> String {
let retVal = self.drugNameTextField.text
let regEx = try! NSRegularExpression(pattern: "", options: .CaseInsensitive)
let match = regEx.matchesInString(retVal!, options: [], range: NSMakeRange(0, (retVal?.characters.count)!))
for zip in match {
let matchRange = zip.range
}
}
I don't understand why I can't just pull the first matching string out!
You can try this out
func match() {
do {
let regex = try NSRegularExpression(pattern: "\\b\\d{5}(?:[ -]\\d{4})?\\b", options: [])
let retVal = "75463 72639823764 gfejwfh56873 89765"
let str = retVal as NSString
let postcodes = regex.matchesInString(retVal,
options: [], range: NSMakeRange(0, retVal.characters.count))
let postcodesArr = postcodes.map { str.substringWithRange($0.range)}
// postcodesArr[0] will give you first postcode
} catch let error as NSError {
}
}
You can use
"\\b\\d{5}(?:[ -]\\d{4})?\\b"
Word boundaries make sure you only match a whole word ZIP.
Backslashes must be doubled.
The hyphen at the end of the character class does not have to be escaped.
To use it:
func regMatchGroup(regex: String, text: String) -> [[String]] {
do {
var resultsFinal = [[String]]()
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text,
options: [], range: NSMakeRange(0, nsString.length))
for result in results {
var internalString = [String]()
for var i = 0; i < result.numberOfRanges; ++i{
internalString.append(nsString.substringWithRange(result.rangeAtIndex(i)))
}
resultsFinal.append(internalString)
}
return resultsFinal
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return [[]]
}
}
let input = "75463 72639823764 gfejwfh56873 89765"
let matches = regMatchGroup("\\b\\d{5}(?:[ -]\\d{4})?\\b", text: input)
if (matches.count > 0)
{
print(matches[0][0]) // Print the first one
}

Swift Get string between 2 strings in a string

I am getting a string from html parse that is;
string = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
my code is something like
var startIndex = text.rangeOfString("'")
var endIndex = text.rangeOfString("',")
var range2 = startIndex2...endIndex
substr= string.substringWithRange(range)
i am not sure if my second splitting string should be "'" or "',"
i want my outcome as
substr = "Info/99/something"
extension String {
func slice(from: String, to: String) -> String? {
return (range(of: from)?.upperBound).flatMap { substringFrom in
(range(of: to, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
String(self[substringFrom..<substringTo])
}
}
}
}
"javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
.sliceFrom("'", to: "',")
I'd use a regular expression to extract substrings from complex input like this.
Swift 3.1:
let test = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
if let match = test.range(of: "(?<=')[^']+", options: .regularExpression) {
print(test.substring(with: match))
}
// Prints: Info/99/something
Swift 2.0:
let test = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
if let match = test.rangeOfString("(?<=')[^']+", options: .RegularExpressionSearch) {
print(test.substringWithRange(match))
}
// Prints: Info/99/something
I rewrote one of the top Swift answers to understand what it was doing with map. I prefer a version using guard, IMO.
extension String {
func slice(from: String, to: String) -> String? {
guard let rangeFrom = range(of: from)?.upperBound else { return nil }
guard let rangeTo = self[rangeFrom...].range(of: to)?.lowerBound else { return nil }
return String(self[rangeFrom..<rangeTo])
}
}
behavior:
let test1 = "a[b]c".slice(from: "[", to: "]") // "b"
let test2 = "abc".slice(from: "[", to: "]") // nil
let test3 = "a]b[c".slice(from: "[", to: "]") // nil
let test4 = "[a[b]c]".slice(from: "[", to: "]") // "a[b"
To find all substrings that are between a starting string and an ending string:
extension String {
func sliceMultipleTimes(from: String, to: String) -> [String] {
components(separatedBy: from).dropFirst().compactMap { sub in
(sub.range(of: to)?.lowerBound).flatMap { endRange in
String(sub[sub.startIndex ..< endRange])
}
}
}
}
let str = "start A end ... start B end"
str.sliceMultipleTimes(from: "start", to: "end") // ["A", "B"]
This works if it is always the second split:
let subString = split(string, isSeparator: "'")[1]
You can use var arr = str.componentsSeparatedByString(",") as your second split which will return you array
Swift 4.2:
extension String {
//right is the first encountered string after left
func between(_ left: String, _ right: String) -> String? {
guard let leftRange = range(of: left), let rightRange = range(of: right, options: .backwards)
,leftRange.upperBound <= rightRange.lowerBound else { return nil }
let sub = self[leftRange.upperBound...]
let closestToLeftRange = sub.range(of: right)!
return String(sub[..<closestToLeftRange.lowerBound])
}
}
Consider using a regular expression to match everything between single quotes.
let string = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
let pattern = "'(.+?)'"
let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)
let results = regex!.matchesInString(string, options: nil, range: NSMakeRange(0, count(string))) as! [NSTextCheckingResult]
let nsstring = string as NSString
let matches = results.map { result in return nsstring.substringWithRange(result.range)}
// First match
println(matches[0])
Swift 5
extension String {
///Returns an empty string when there is no path.
func substring(from left: String, to right: String) -> String {
if let match = range(of: "(?<=\(left))[^\(right)]+", options: .regularExpression) {
return String(self[match])
}
return ""
}
}
In Swift 4 or later you can create an extension method on StringProtocol to support substrings as well. You can just return a Substring instead of a new String:
edit/update: Swift 5 or later
extension StringProtocol {
func substring<S: StringProtocol>(from start: S, options: String.CompareOptions = []) -> SubSequence? {
guard let lower = range(of: start, options: options)?.upperBound
else { return nil }
return self[lower...]
}
func substring<S: StringProtocol>(through end: S, options: String.CompareOptions = []) -> SubSequence? {
guard let upper = range(of: end, options: options)?.upperBound
else { return nil }
return self[..<upper]
}
func substring<S: StringProtocol>(upTo end: S, options: String.CompareOptions = []) -> SubSequence? {
guard let upper = range(of: end, options: options)?.lowerBound
else { return nil }
return self[..<upper]
}
func substring<S: StringProtocol, T: StringProtocol>(from start: S, upTo end: T, options: String.CompareOptions = []) -> SubSequence? {
guard let lower = range(of: start, options: options)?.upperBound,
let upper = self[lower...].range(of: end, options: options)?.lowerBound
else { return nil }
return self[lower..<upper]
}
func substring<S: StringProtocol, T: StringProtocol>(from start: S, through end: T, options: String.CompareOptions = []) -> SubSequence? {
guard let lower = range(of: start, options: options)?.upperBound,
let upper = self[lower...].range(of: end, options: options)?.upperBound
else { return nil }
return self[lower..<upper]
}
}
Usage:
let string = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
let substr = string.substring(from: "'") // "Info/99/something', 'City Hall',1, 99);"
let through = string.substring(through: "Info") // "javascript:getInfo"
let upTo = string.substring(upTo: "Info") // "javascript:get"
let fromUpTo = string.substring(from: "'", upTo: "',") // "Info/99/something"
let fromThrough = string.substring(from: "'", through: "',") // "Info/99/something',"
let fromUpToCaseInsensitive = string.substring(from: "'info/", upTo: "/something", options: .caseInsensitive) // "99"
If you want to support also from the start or end of the string
extension String {
func slice(from: String, to: String) -> String? {
return (from.isEmpty ? startIndex..<startIndex : range(of: from)).flatMap { fromRange in
(to.isEmpty ? endIndex..<endIndex : range(of: to, range: fromRange.upperBound..<endIndex)).map({ toRange in
String(self[fromRange.upperBound..<toRange.lowerBound])
})
}
}
}
"javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
.slice(from: "'", to: "',") // "Info/99/something"
"javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
.slice(from: "", to: ":") // "javascript"
"javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
.slice(from: ":", to: "") // "getInfo(1,'Info/99/something', 'City Hall',1, 99);"
"javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
.slice(from: "", to: "") // "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);"
if you want another syntax, maybe more readable
extension String {
func slice(from: String, to: String) -> String? {
guard let fromRange = from.isEmpty ? startIndex..<startIndex : range(of: from) else { return nil }
guard let toRange = to.isEmpty ? endIndex..<endIndex : range(of: to, range: fromRange.upperBound..<endIndex) else { return nil }
return String(self[fromRange.upperBound..<toRange.lowerBound])
}
}
Swift 4 version of #litso. To find all values in text
func find(inText text: String, pattern: String) -> [String]? {
do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let result = regex.matches(in: text, options: .init(rawValue: 0), range: NSRange(location: 0, length: text.count))
let matches = result.map { result in
return (text as NSString).substring(with: result.range)
}
return matches
} catch {
print(error)
}
return nil
}

Swift extract regex matches

I want to extract substrings from a string that match a regex pattern.
So I'm looking for something like this:
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
???
}
So this is what I have:
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
var regex = NSRegularExpression(pattern: regex,
options: nil, error: nil)
var results = regex.matchesInString(text,
options: nil, range: NSMakeRange(0, countElements(text)))
as Array<NSTextCheckingResult>
/// ???
return ...
}
The problem is, that matchesInString delivers me an array of NSTextCheckingResult, where NSTextCheckingResult.range is of type NSRange.
NSRange is incompatible with Range<String.Index>, so it prevents me of using text.substringWithRange(...)
Any idea how to achieve this simple thing in swift without too many lines of code?
Even if the matchesInString() method takes a String as the first argument,
it works internally with NSString, and the range parameter must be given
using the NSString length and not as the Swift string length. Otherwise it will
fail for "extended grapheme clusters" such as "flags".
As of Swift 4 (Xcode 9), the Swift standard
library provides functions to convert between Range<String.Index>
and NSRange.
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: text,
range: NSRange(text.startIndex..., in: text))
return results.map {
String(text[Range($0.range, in: text)!])
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
Example:
let string = "🇩🇪€4€9"
let matched = matches(for: "[0-9]", in: string)
print(matched)
// ["4", "9"]
Note: The forced unwrap Range($0.range, in: text)! is safe because
the NSRange refers to a substring of the given string text.
However, if you want to avoid it then use
return results.flatMap {
Range($0.range, in: text).map { String(text[$0]) }
}
instead.
(Older answer for Swift 3 and earlier:)
So you should convert the given Swift string to an NSString and then extract the
ranges. The result will be converted to a Swift string array automatically.
(The code for Swift 1.2 can be found in the edit history.)
Swift 2 (Xcode 7.3.1) :
func matchesForRegexInText(regex: String, text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text,
options: [], range: NSMakeRange(0, nsString.length))
return results.map { nsString.substringWithRange($0.range)}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
Example:
let string = "🇩🇪€4€9"
let matches = matchesForRegexInText("[0-9]", text: string)
print(matches)
// ["4", "9"]
Swift 3 (Xcode 8)
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range)}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
Example:
let string = "🇩🇪€4€9"
let matched = matches(for: "[0-9]", in: string)
print(matched)
// ["4", "9"]
My answer builds on top of given answers but makes regex matching more robust by adding additional support:
Returns not only matches but returns also all capturing groups for each match (see examples below)
Instead of returning an empty array, this solution supports optional matches
Avoids do/catch by not printing to the console and makes use of the guard construct
Adds matchingStrings as an extension to String
Swift 4.2
//: Playground - noun: a place where people can play
import Foundation
extension String {
func matchingStrings(regex: String) -> [[String]] {
guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] }
let nsString = self as NSString
let results = regex.matches(in: self, options: [], range: NSMakeRange(0, nsString.length))
return results.map { result in
(0..<result.numberOfRanges).map {
result.range(at: $0).location != NSNotFound
? nsString.substring(with: result.range(at: $0))
: ""
}
}
}
}
"prefix12 aaa3 prefix45".matchingStrings(regex: "fix([0-9])([0-9])")
// Prints: [["fix12", "1", "2"], ["fix45", "4", "5"]]
"prefix12".matchingStrings(regex: "(?:prefix)?([0-9]+)")
// Prints: [["prefix12", "12"]]
"12".matchingStrings(regex: "(?:prefix)?([0-9]+)")
// Prints: [["12", "12"]], other answers return an empty array here
// Safely accessing the capture of the first match (if any):
let number = "prefix12suffix".matchingStrings(regex: "fix([0-9]+)su").first?[1]
// Prints: Optional("12")
Swift 3
//: Playground - noun: a place where people can play
import Foundation
extension String {
func matchingStrings(regex: String) -> [[String]] {
guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] }
let nsString = self as NSString
let results = regex.matches(in: self, options: [], range: NSMakeRange(0, nsString.length))
return results.map { result in
(0..<result.numberOfRanges).map {
result.rangeAt($0).location != NSNotFound
? nsString.substring(with: result.rangeAt($0))
: ""
}
}
}
}
"prefix12 aaa3 prefix45".matchingStrings(regex: "fix([0-9])([0-9])")
// Prints: [["fix12", "1", "2"], ["fix45", "4", "5"]]
"prefix12".matchingStrings(regex: "(?:prefix)?([0-9]+)")
// Prints: [["prefix12", "12"]]
"12".matchingStrings(regex: "(?:prefix)?([0-9]+)")
// Prints: [["12", "12"]], other answers return an empty array here
// Safely accessing the capture of the first match (if any):
let number = "prefix12suffix".matchingStrings(regex: "fix([0-9]+)su").first?[1]
// Prints: Optional("12")
Swift 2
extension String {
func matchingStrings(regex: String) -> [[String]] {
guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] }
let nsString = self as NSString
let results = regex.matchesInString(self, options: [], range: NSMakeRange(0, nsString.length))
return results.map { result in
(0..<result.numberOfRanges).map {
result.rangeAtIndex($0).location != NSNotFound
? nsString.substringWithRange(result.rangeAtIndex($0))
: ""
}
}
}
}
The fastest way to return all matches and capture groups in Swift 5
extension String {
func match(_ regex: String) -> [[String]] {
let nsString = self as NSString
return (try? NSRegularExpression(pattern: regex, options: []))?.matches(in: self, options: [], range: NSMakeRange(0, nsString.length)).map { match in
(0..<match.numberOfRanges).map { match.range(at: $0).location == NSNotFound ? "" : nsString.substring(with: match.range(at: $0)) }
} ?? []
}
}
Returns a 2-dimentional array of strings:
"prefix12suffix fix1su".match("fix([0-9]+)su")
returns...
[["fix12su", "12"], ["fix1su", "1"]]
// First element of sub-array is the match
// All subsequent elements are the capture groups
If you want to extract substrings from a String, not just the position, (but the actual String including emojis). Then, the following maybe a simpler solution.
extension String {
func regex (pattern: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions(rawValue: 0))
let nsstr = self as NSString
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatchesInString(self, options: NSMatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let result = nsstr.substringWithRange(r.range) as String
matches.append(result)
}
}
return matches
} catch {
return [String]()
}
}
}
Example Usage:
"someText 👿🏅👿⚽️ pig".regex("👿⚽️")
Will return the following:
["👿⚽️"]
Note using "\w+" may produce an unexpected ""
"someText 👿🏅👿⚽️ pig".regex("\\w+")
Will return this String array
["someText", "️", "pig"]
I found that the accepted answer's solution unfortunately does not compile on Swift 3 for Linux. Here's a modified version, then, that does:
import Foundation
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try RegularExpression(pattern: regex, options: [])
let nsString = NSString(string: text)
let results = regex.matches(in: text, options: [], range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range) }
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
The main differences are:
Swift on Linux seems to require dropping the NS prefix on Foundation objects for which there is no Swift-native equivalent. (See Swift evolution proposal #86.)
Swift on Linux also requires specifying the options arguments for both the RegularExpression initialization and the matches method.
For some reason, coercing a String into an NSString doesn't work in Swift on Linux but initializing a new NSString with a String as the source does work.
This version also works with Swift 3 on macOS / Xcode with the sole exception that you must use the name NSRegularExpression instead of RegularExpression.
Swift 4 without NSString.
extension String {
func matches(regex: String) -> [String] {
guard let regex = try? NSRegularExpression(pattern: regex, options: [.caseInsensitive]) else { return [] }
let matches = regex.matches(in: self, options: [], range: NSMakeRange(0, self.count))
return matches.map { match in
return String(self[Range(match.range, in: self)!])
}
}
}
#p4bloch if you want to capture results from a series of capture parentheses, then you need to use the rangeAtIndex(index) method of NSTextCheckingResult, instead of range. Here's #MartinR 's method for Swift2 from above, adapted for capture parentheses. In the array that is returned, the first result [0] is the entire capture, and then individual capture groups begin from [1]. I commented out the map operation (so it's easier to see what I changed) and replaced it with nested loops.
func matches(for regex: String!, in text: String!) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text, options: [], range: NSMakeRange(0, nsString.length))
var match = [String]()
for result in results {
for i in 0..<result.numberOfRanges {
match.append(nsString.substringWithRange( result.rangeAtIndex(i) ))
}
}
return match
//return results.map { nsString.substringWithRange( $0.range )} //rangeAtIndex(0)
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
An example use case might be, say you want to split a string of title year eg "Finding Dory 2016" you could do this:
print ( matches(for: "^(.+)\\s(\\d{4})" , in: "Finding Dory 2016"))
// ["Finding Dory 2016", "Finding Dory", "2016"]
Most of the solutions above only give the full match as a result ignoring the capture groups e.g.: ^\d+\s+(\d+)
To get the capture group matches as expected you need something like (Swift4) :
public extension String {
public func capturedGroups(withRegex pattern: String) -> [String] {
var results = [String]()
var regex: NSRegularExpression
do {
regex = try NSRegularExpression(pattern: pattern, options: [])
} catch {
return results
}
let matches = regex.matches(in: self, options: [], range: NSRange(location:0, length: self.count))
guard let match = matches.first else { return results }
let lastRangeIndex = match.numberOfRanges - 1
guard lastRangeIndex >= 1 else { return results }
for i in 1...lastRangeIndex {
let capturedGroupIndex = match.range(at: i)
let matchedString = (self as NSString).substring(with: capturedGroupIndex)
results.append(matchedString)
}
return results
}
}
Update for iOS 16: Regex, RegexBuilder 👷‍♀️
Xcode previously supported Regex with the Find and Search tab. Many found Apple's NSRegularExpressions Swift API verbose and unwieldy, so Apple released Regex literal support and RegexBuilder this year.
The API has been simplified going forward to tidy up complex String range-based parsing logic in iOS 16 / macOS 13 as well as improve performance.
RegEx literals in Swift 5.7
func parseLine(_ line: Substring) throws -> MailmapEntry {
let regex = /\h*([^<#]+?)??\h*<([^>#]+)>\h*(?:#|\Z)/
guard let match = line.prefixMatch(of: regex) else {
throw MailmapError.badLine
}
return MailmapEntry(name: match.1, email: match.2)
}
At the moment, we are able to match using prefixMatch or wholeMatch to find a single match, but the API may improve in the future for multiple matches.
RegexBuilder in Swift 5.7
RegexBuilder is a new API released by Apple aimed at making RegEx code easier to write in Swift. We can translate the Regex literal /\h*([^<#]+?)??\h*<([^>#]+)>\h*(?:#|\Z)/ from above into a more declarative form using RegexBuilder if we want more readability.
Do note that we can use raw strings in a RegexBuilder and also interleave Regex Literals in the builder if we want to balance readability with conciseness.
import RegexBuilder
let regex = Regex {
ZeroOrMore(.horizontalWhitespace)
Optionally {
Capture(OneOrMore(.noneOf("<#")))
}
.repetitionBehavior(.reluctant)
ZeroOrMore(.horizontalWhitespace)
"<"
Capture(OneOrMore(.noneOf(">#")))
">"
ZeroOrMore(.horizontalWhitespace)
/#|\Z/
}
The RegEx literal /£|\Z/ is equivalent to:
ChoiceOf {
"#"
Anchor.endOfSubjectBeforeNewline
}
Composable RegexComponent
RegexBuilder syntax is similar to SwiftUI also in terms of composability because we can reuse RegexComponents within other RegexComponents:
struct MailmapLine: RegexComponent {
#RegexComponentBuilder
var regex: Regex<(Substring, Substring?, Substring)> {
ZeroOrMore(.horizontalWhitespace)
Optionally {
Capture(OneOrMore(.noneOf("<#")))
}
.repetitionBehavior(.reluctant)
ZeroOrMore(.horizontalWhitespace)
"<"
Capture(OneOrMore(.noneOf(">#")))
">"
ZeroOrMore(.horizontalWhitespace)
ChoiceOf {
"#"
Anchor.endOfSubjectBeforeNewline
}
}
}
This is how I did it, I hope it brings a new perspective how this works on Swift.
In this example below I will get the any string between []
var sample = "this is an [hello] amazing [world]"
var regex = NSRegularExpression(pattern: "\\[.+?\\]"
, options: NSRegularExpressionOptions.CaseInsensitive
, error: nil)
var matches = regex?.matchesInString(sample, options: nil
, range: NSMakeRange(0, countElements(sample))) as Array<NSTextCheckingResult>
for match in matches {
let r = (sample as NSString).substringWithRange(match.range)//cast to NSString is required to match range format.
println("found= \(r)")
}
This is a very simple solution that returns an array of string with the matches
Swift 3.
internal func stringsMatching(regularExpressionPattern: String, options: NSRegularExpression.Options = []) -> [String] {
guard let regex = try? NSRegularExpression(pattern: regularExpressionPattern, options: options) else {
return []
}
let nsString = self as NSString
let results = regex.matches(in: self, options: [], range: NSMakeRange(0, nsString.length))
return results.map {
nsString.substring(with: $0.range)
}
}
update #Mike Chirico's to Swift 5
extension String{
func regex(pattern: String) -> [String]?{
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: 0))
let all = NSRange(location: 0, length: count)
var matches = [String]()
regex.enumerateMatches(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let nsstr = self as NSString
let result = nsstr.substring(with: r.range) as String
matches.append(result)
}
}
return matches
} catch {
return nil
}
}
}
basic phone number matching
let phoneNumbers = ["+79990001101", "+7 (800) 000-11-02", "+34 507 574 147 ", "+1-202-555-0118"]
let match: (String) -> String = {
$0.replacingOccurrences(of: #"[^\d+]"#, with: "", options: .regularExpression)
}
print(phoneNumbers.map(match))
// ["+79990001101", "+78000001102", "+34507574147", "+12025550118"]
Big thanks to Lars Blumberg his answer for capturing groups and full matches with Swift 4, which helped me out a lot. I also made an addition to it for the people who do want an error.localizedDescription response when their regex is invalid:
extension String {
func matchingStrings(regex: String) -> [[String]] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = self as NSString
let results = regex.matches(in: self, options: [], range: NSMakeRange(0, nsString.length))
return results.map { result in
(0..<result.numberOfRanges).map {
result.range(at: $0).location != NSNotFound
? nsString.substring(with: result.range(at: $0))
: ""
}
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
}
For me having the localizedDescription as error helped understand what went wrong with escaping, since it's displays which final regex swift tries to implement.
You can use matching(regex:) on the string like:
let array = try "Your String To Search".matching(regex: ".")
using this simple extension:
public extension String {
func matching(regex: String) throws -> [String] {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: self, range: NSRange(startIndex..., in: self))
return results.map { String(self[Range($0.range, in: self)!]) }
}
}

Resources