Getting a empty Range<String.Index> in Swift - swift

I am very new to Swift and have trying to use regular expressions, but getting the match from the string seems to be an insurmountable task.
This is my current approach.
print(data.substring(with: (data.range(of: "[a-zA-Z]at", options: .regularExpression))))
This doesn't work because
Value of optional type 'Range<String.Index>?' must be unwrapped to a value of type 'Range<String.Index>'
I guess this has something to do with it possibly being null, so now i want to provide it with an alternative using the ?? operator.
print(data.substring(with: (data.range(of: "[a-zA-Z]at", options: .regularExpression) ?? Range<String.Index>())))
What i want to do is to provide it with an empty range object but it seems to be impossible to create an empty object of the type required.
Any suggestions?

There is simply no argument-less initialiser for Range<String.Index>.
One way you can create an empty range of String.Index is to use:
data.startIndex..<data.startIndex
Remember that you shouldn't use integers here, because we are dealing with indices of a string. See this if you don't understand why.
So:
print(data.substring(with: (data.range(of: "[a-zA-Z]at", options: .regularExpression) ?? data.startIndex..<data.startIndex)))
But substring(with:) is deprecated. You are recommended to use the subscript:
print(data[data.range(of: "[a-zA-Z]at", options: .regularExpression) ?? data.startIndex..<data.startIndex])

Instead of trying to create an empty range, I would suggest creating an empty Substring in case there was no match. Range can be quite error-prone, so using this approach you can save yourself a lot of headaches.
let match: Substring
if let range = data.range(of: "[a-zA-Z]at", options: .regularExpression) {
match = data[range]
} else {
match = ""
}
print(match)

You can create such constructor with a simple extension to Range type. Like this:
extension Range where Bound == String.Index {
static var empty: Range<Bound> {
"".startIndex..<"".startIndex
}
init() {
self = Range<Bound>.empty
}
}
Then it can be used like this:
let str = "kukukukuku"
let substr1 = str[str.range(of: "abc", options: .regularExpression) ?? Range<String.Index>()]
let substr2 = str[str.range(of: "abc", options: .regularExpression) ?? Range<String.Index>.empty]

Related

Swift replace occurrence of string with condition

I have string like below
<p><strong>I am a strongPerson</strong></p>
I want to covert this string like this
<p><strong>I am a weakPerson</strong></p>
When I try below code
let old = "<p><strong>I am a strongPerson</strong></p>"
let new = old.replacingOccurrences(of: "strong", with: "weak")
print("\(new)")
I am getting output like
<p><weak>I am a weakPerson</weak></p>
But I need output like this
<p><strong>I am a weakPerson</strong></p>
My Condition here is
1.It has to replace only if word does not contain these HTML Tags like "<>".
Help me to get it. Thanks in advance.
You can use a regular expression to avoid the word being in a tag:
let old = "strong <p><strong>I am a strong person</strong></p> strong"
let new = old.replacingOccurrences(of: "strong(?!>)", with: "weak", options: .regularExpression, range: nil)
print(new)
I added some extra uses of the word "strong" to test edge cases.
The trick is the use of (?!>) which basically means to ignore any match that has a > at the end of it. Look at the documentation for NSRegularExpression and find the documentation for the "negative look-ahead assertion".
Output:
weak <p><strong>I am a weak person</strong></p> weak
Try the following:
let myString = "<p><strong>I am a strongPerson</strong></p>"
if let regex = try? NSRegularExpression(pattern: "strong(?!>)") {
let modString = regex.stringByReplacingMatches(in: myString, options: [], range: NSRange(location: 0, length: myString.count), withTemplate: "weak")
print(modString)
}

Swift: Checking Prefix with Regex

I am attempting to check if a string begins with a text that follows a regex pattern. Is there a way to do so using string.hasPrefix()?
My implementation so far:
let pattern = "[Ff][Yy][Ii](.)?"
let regex = try? NSRegularExpression(pattern: pattern, options: [])
if firstRowText.hasPrefix(regex) { //Cannot convert value of type NSRegularExpression to String
}
You should use a regex using range(of:options:range:locale:) passing the .regularExpression option with .anchored option:
if firstRowText.range(of: "[Ff][Yy][Ii]", options: [.regularExpression, .anchored]) != nil {...}
The .anchored option makes the regex engine search for a match at the start of the string only.
To make your regex match in a case insensitive way, you may pass another option alongside the current ones, .caseInsensitive, and use a shorter regex, like "FYI":
if firstRowText.range(of: "FYI", options: [.regularExpression, .anchored, .caseInsensitive]) != nil {
See the Swift online demo.
Note that you may also use an inline modifier option (?i) to set case insensitivity:
"(?i)FYI"

how to run multiples NSRegularExpression once

I have a bunch of NSRegularExpression and I want to run it once. Anyone knows how to do it ?
For the moment I do it in a .forEach, for performance reasons I do not think this is the best idea
Each NSRegularExpression needs to match a different pattern, after the matching I need to deal with each different kind of match. As example if I match with the first regex in my array I need to make something different from the second etc...
let test: String = "Stuff"
let range: NSRange = // a range
var regexes = [NSRegularExpression] = // all of my regexes
regexes.forEach { $0.matches(in: text, options: [], range: range) }
Thanks for you help
You may be able to evaluate several regular expressions as one if you concatenate them using capture groups and an OR expressions.
If you want to search for: language, Objective-C and Swift strings you should use a pattern like this: (language)|(Objective-C)|(Swift). Each capture group has an order number, so if language is found in the source string the match object provides the index number.
You can used the code in this playground sample:
import Foundation
let sourceString: String = "Swift is a great language to program, but don't forget Objective-C."
let expresions = [ "language", // Expression 0
"Objective-C", // Expression 1
"Swift" // Expression 2
]
let pattern = expresions
.map { "(\($0))" }
.joined(separator: "|") // pattern is defined as : (language)|(Objective-C)|(Swift)
let regex = try? NSRegularExpression(pattern: pattern, options: [])
let matches = regex?.matches(in: sourceString, options: [],
range: NSRange(location: 0, length: sourceString.utf16.count))
let results = matches?.map({ (match) -> (Int, String) in // Array of type (Int: String) which
// represents index of expression and
// string capture
let index = (1...match.numberOfRanges-1) // Go through all ranges to test which one was used
.map{ Range(match.range(at: $0), in: sourceString) != nil ? $0 : nil }
.compactMap { $0 }.first! // Previous map return array with nils and just one Int
// with the correct position, lets apply compactMap to
// get just this number
let foundString = String(sourceString[Range(match.range(at: 0), in: sourceString)!])
let position = match.range(at: 0).location
let niceReponse = "\(foundString) [position: \(position)]"
return (index - 1, niceReponse) // Let's substract 1 to index in order to match zero based array index
})
print("Matches: \(results?.count ?? 0)\n")
results?.forEach({ result in
print("Group \(result.0): \(result.1)")
})
If you run it the result is:
How many matches: 3
Expression 2: Swift [position: 0]
Expression 0: language [position: 17]
Expression 1: Objective-C [position: 55]
I hope I understood correctly your question and this code helps you.

Swift 3 replacingOccurrences of multiple strings

I have JavaScript code that looks like the following:
foo.replace(/(\r\n|\n|\r)/gm, "");
Basically this strips any return carriages and new lines from my string. I'd like to do something similar in Swift 3.
I see func replacingOccurrences(of target: String, with replacement: String, options: CompareOptions = default, range searchRange: Range<Index>? = default) -> String is available. The problem is, this only takes one string in the of parameter.
Does this mean I need to call the method multiple times for each of my instances above, once for \r\n, once for \n\ and once for \r? Is there anyway to potentially accomplish something closer to what the regex is doing instead of calling replacingOccurrences three times?
Use replacingOccurrences with the option set to regularExpression.
let updated = foo.replacingOccurrences(of: "\r\n|\n|\r", with: "", options: .regularExpression)
To replace non-digits you can use "\D" as a regular expression
let numbers = "+1(202)-505-71-17".replacingOccurrences(of: "\\D", with: "", options: .regularExpression)
You can find how to use other regular expressions here
This one is pretty easy, it's all in the documentation.
let noNewlines = "a\nb\r\nc\r".replacingOccurrences(of: "\r\n|\n|\r", with: "", options: .regularExpression)

Substrings in Swift

I'm having a problem with understand how I can work with substrings in Swift. Basically, I'm getting a JSON value that has a string with the following format:
Something
I'm trying to get rid of the HTML anchor tag with Swift so I'm left with Something. My thought was to find the index of every < and > in the string so then I could just do a substringWithRange and advance up to the right index.
My problem is that I can't figure out how to find the index. I've read that Swift doesn't support the index (unless you extend it.)
I don't want to add CPU cycles unnecessarily. So my question is, how do I find the indexes in a way that is not inefficient? Or, is there a better way of filtering out the tags?
Edit: Converted Andrew's first code sample to a function:
func formatTwitterSource(rawStr: String) -> String {
let unParsedString = rawStr
var midParseString = ""
var parsedString = ""
if let firstEndIndex = find(unParsedString, ">") {
midParseString = unParsedString[Range<String.Index>(start: firstEndIndex.successor(), end: unParsedString.endIndex)]
if let secondStartIndex = find(midParseString, "<") {
parsedString = midParseString[Range<String.Index>(start: midParseString.startIndex, end: secondStartIndex)]
}
}
return parsedString
}
Nothing too complicated. It takes in a String that has the tags in it. Then it uses Andrew's magic to parse everything out. I renamed the variables and made them clearer so you can see which variable does what in the process. Then in the end, it returns the parsed string.
You could do something like this, but it isn't pretty really. Obviously you would want to factor this into a function and possibly allow for various start/end tokens.
let testText = "Something"
if let firstEndIndex = find(testText, ">") {
let testText2 = testText[Range<String.Index>(start: firstEndIndex.successor(), end: testText.endIndex)]
if let secondStartIndex = find(testText2, "<") {
let testText3 = testText2[Range<String.Index>(start: testText2.startIndex, end: secondStartIndex)]
}
}
Edit
Working on this a little further and came up with something a little more idiomatic?
let startSplits = split(testText, { $0 == "<" })
let strippedValues = map(startSplits) { (s) -> String? in
if let endIndex = find(s, ">") {
return s[Range<String.Index>(start: endIndex.successor(), end: s.endIndex)]
}
return nil
}
let strings = map(filter(strippedValues, { $0 != "" })) { $0! }
It uses a little more functional style there at the end. Not sure I much enjoy the Swift style of map/filter compared to Haskell. But anyhow, the one potentially dangerous part is that forced unwrapping in the final map. If you can live with a result of [String?] then it isn't necessary.
Even though this question has been already answered, I am adding solution based on regex.
let pattern = "<.*>(.*)<.*>"
let src = "Something"
var error: NSError? = nil
var regex = NSRegularExpression(pattern: pattern, options: .DotMatchesLineSeparators, error: &error)
if let regex = regex {
var result = regex.stringByReplacingMatchesInString(src, options: nil, range: NSRange(location:0,
length:countElements(src)), withTemplate: "$1")
println(result)
}