NSAttributedString: turn (multiple) #username[userid] mentions into clickable #username links - swift

I am in the process of writing code to display mentions within an NSAttributedString, which need to link out to a user profile. The format of the mentions is #username[userid], which would need to be displayed as simply #username, which is tappable.
I have the code working so far that the username becomes clickable, but I now need to remove the [userid] part, which of course modifies the length of the string so that ranges don't match anymore, etc. Not sure how I can solve this.
import Foundation
import UIKit
let comment = "Hello #kevin[1], #john and #andrew[2]!"
let wholeRange = NSRange(comment.startIndex..<comment.endIndex, in: comment)
let regex = try NSRegularExpression(pattern: #"(#[\w.-#]+)\[(\d+)\]"#)
let attributedString = NSMutableAttributedString(string: comment)
regex.enumerateMatches(in: comment, options: [], range: wholeRange) { match, _, _ in
guard let match = match else {
return
}
let userIdRange = Range(match.range(at: 2), in: comment)!
let userId = comment[userIdRange]
let usernameRange = match.range(at: 1)
attributedString.addAttribute(NSAttributedString.Key.link, value: URL(string: "test://profile/\(userId)")!, range: usernameRange)
}
print(attributedString)
The result right now can be represented like this, when printed:
Hello {
}#kevin{
NSLink = "test://profile/1";
}[1], #john and {
}#andrew{
NSLink = "test://profile/2";
}[2]!{
}
So #kevin and #andrew are links, #john is not (which is expected!), but the user ids are still visible. Surely this is a problem that has been solved before but I can't find any examples, not sure what keywords to search for. There are plenty of questions about detecting usernames/mentions in strings, and even more about making links in NSAttributedString, but that's not the problem I am trying to solve.
How would I turn the #username[userid] mentions into clickable #username links, so that the [userid] part is hidden?

You just need to get all the matching ranges, iterate them in reverse order, add the link to it and then replace the whole range with the name. Something like:
let comment = "Hello #kevin[1], #john and #andrew[2]!"
let attributedString = NSMutableAttributedString(string: comment)
let regex = try NSRegularExpression(pattern: #"(#[\w.-#]+)\[(\d+)\]"#)
var ranges: [(NSRange,NSRange,NSRange)] = []
regex.enumerateMatches(in: comment, range: NSRange(comment.startIndex..., in: comment)) { match, _, _ in
guard let match = match else {
return
}
ranges.append((match.range(at: 0),
match.range(at: 1),
match.range(at: 2)))
}
ranges.reversed().forEach {
let userId = attributedString.attributedSubstring(from: $0.2).string
let username = attributedString.attributedSubstring(from: $0.1).string
attributedString.addAttribute(.link, value: URL(string: "test://profile/\(userId)")!, range: $0.0)
attributedString.replaceCharacters(in: $0.0, with: username)
}
print(attributedString)
This will print
Hello {
}#kevin{
NSLink = "test://profile/1";
}, #john and {
}#andrew{
NSLink = "test://profile/2";
}!{
}

Quickly done:
let comment = "Hello #kevin[1], #john and #andrew[2]!"
let attributedString = NSMutableAttributedString(string: comment)
let wholeRange = NSRange(attributedString.string.startIndex..<attributedString.string.endIndex, in: attributedString.string)
let regex = try NSRegularExpression(pattern: #"(#[\w.-#]+)\[(\d+)\]"#)
let matches = regex.matches(in: attributedString.string, options: [], range: wholeRange)
matches.reversed().forEach { aResult in
let fullMatchRange = Range(aResult.range(at: 0), in: attributedString.string)! //#kevin[1]
let replacementRange = Range(aResult.range(at: 1), in: attributedString.string)! //#kevin
let userIdRange = Range(aResult.range(at: 2), in: attributedString.string)! // 1
let atAuthor = String(attributedString.string[replacementRange])
attributedString.addAttribute(.link,
value: URL(string: "test://profile/\(attributedString.string[userIdRange])")!,
range: NSRange(fullMatchRange, in: attributedString.string))
attributedString.replaceCharacters(in: NSRange(fullMatchRange, in: attributedString.string),
with: atAuthor)
}
print(attributedString)
Output:
Hello {
}#kevin{
NSLink = "test://profile/1";
}, #john and {
}#andrew{
NSLink = "test://profile/2";
}!{
}
What's to see:
I changed the pattern, for easy captures. See the sample in comment in the forEach().
I used matches in reverse order, else the ranges won't be accurate anymore!
I kept playing with attributedString.string instead of comment in case it's "unsync".

Related

Replace in string with regex

I am struggling to modify captured value with regex.
For example, I wanna change "Hello, he is hero" to "HEllo, HE is HEro" using Regex.
I know there are ways to change this without regex, but it is just an example to show the problem. I actually use the regex instead of just he, but I cannot provide it here. That is why using regex is required.
The code below somehow does not work. Are there any ways to make it work?
"Hello, he is hero".replacingOccurrences(
of: #"(he)"#,
with: "$1".uppercased(), // <- uppercased is not applied
options: .regularExpression
)
You need to use your regex in combination with Range (range(of:)) to find matches and then replace each found range separately
Here is a function as an extension to String that does this by using range(of:) starting from the start of the string and then moving the start index to match from forward to after the last match. The actual replacement is done inside a separate function that is passed as an argument
extension String {
func replace(regex: String, with replace: (Substring) -> String) -> String {
var string = self
var startIndex = self.startIndex
let endIndex = self.endIndex
while let range = string.range(of: regex, options: [.regularExpression] , range: startIndex..<endIndex) {
if range.isEmpty {
startIndex = string.index(startIndex, offsetBy: 1)
if startIndex >= endIndex { break }
continue
}
string.replaceSubrange(range, with: replace(string[range]))
startIndex = range.upperBound
}
return string
}
}
Example where we do an case insensitive search for words starting with "he" and replace each match with the uppercased version
let result = "Hello, he is hero. There he is".replace(regex: #"(?i)\bhe"#) {
$0.uppercased()
}
Output
HEllo, HE is HEro. There HE is
You can try NSRegularExpression. Something like:
import Foundation
var sourceStr = "Hello, he is hero"
let regex = try! NSRegularExpression(pattern: "(he)")
let matches = regex.matches(in: sourceStr, range: NSRange(sourceStr.startIndex..., in: sourceStr))
regex.enumerateMatches(in: sourceStr, range: NSRange(sourceStr.startIndex..., in: sourceStr)) { (match, _, _) in
guard let match = match else { return }
guard let range = Range(match.range, in: sourceStr) else { return }
let sub = sourceStr[range]
sourceStr = sourceStr.replacingOccurrences(of: sub, with: sub.uppercased(), options: [], range: range)
}
print(sourceStr)
this is the solution i can provide
var string = "Hello, he is hero"
let occurrence = "he"
string = string.lowercased().replacingOccurrences(
of: occurrence,
with: occurrence.uppercased(),
options: .regularExpression
)
print(string)

regular expressions: simplified form?

I'm finally learning swift. The documentation that I've seen for regex in swift consist of something like the following:
let testString = "hat"
let range = NSRange(location: 0, length: testString.utf16.count)
let regex = try! NSRegularExpression(pattern: "[a-z]at")
let r = regex.firstMatch(in: testString, options: [], range: range) != nil
print("ns-based regex match?: ", r)
Is this the preferred/only way of doing this or is there an updated technique?
It's a bit verbose.
We’d often just use range(of:options:range:locale:) with a .regularExpression:
let testString = "foo hat"
if let range = testString.range(of: "[a-z]at", options: .regularExpression) {
print(testString[range])
}
If you don't need some of the more advanced NSRegularExpression options, the above is bit simpler.

how to get a word before specific keyword in a sentence in Swift?

Aipda Karel Sasuit Tubun No.106B, RT.2/RW.1, Ps. Baru, Karawaci, Kota
Tangerang, Banten 15112, Indonesia"
I have an string address like that, and I want to get the postal code 15112 that always positioned right before ', Indonesia'
I am a beginner, I am sorry if this is trivial since I can't find it in Stackoverflow
let address = "Aipda Karel Sasuit Tubun No.106B, RT.2/RW.1, Ps. Baru, Karawaci, Kota Tangerang, Banten 15112, Indonesia"
func postcode(from address: String, for area: String) -> String? {
let array = address.replacingOccurrences(of: ",", with: "").components(separatedBy: " ")
if let index = array.lastIndex(where: { $0.contains(area) }) {
return array[index - 1]
}
return nil
}
postcode(from: address, for: "Indonesia")
Sorry I think it's a bit of a long winded method but this should work:
let address = "Aipda Karel Sasuit Tubun No.106B, RT.2/RW.1, Ps. Baru, Karawaci, Kota Tangerang, Banten 15112, Indonesia"
let keyword = "Indonesia"
let components: [String] = address.split(separator: " ").map({ String($0).replacingOccurrences(of: ",", with: "") })
var postCode = components.first ?? ""
for comp in components {
if comp == keyword {
break
}
postCode = comp
}
print(postCode)
You could use a regular expression :
let str = "Aipda Karel Sasuit Tubun No.106B, RT.2/RW.1, Ps. Baru, Karawaci, Kota Tangerang, Banten 15112, Indonesia"
let range = NSRange(location: 0, length: str.utf16.count)
let regex = try! NSRegularExpression(pattern: "[0-9]+(?=, Indonesia)")
if let match = regex.firstMatch(in: str, range: range) {
let subStr = String(str[Range(match.range, in: str)!])
print(subStr) //15112
}

Replace string between characters in swift

I have many strings, like this:
'This is a "table". There is an "apple" on the "table".'
I want to replace "table", "apple" and "table" with spaces. Is there a way to do it?
A simple regular expression:
let sentence = "This is \"table\". There is an \"apple\" on the \"table\""
let pattern = "\"[^\"]+\"" //everything between " and "
let replacement = "____"
let newSentence = sentence.replacingOccurrences(
of: pattern,
with: replacement,
options: .regularExpression
)
print(newSentence) // This is ____. There is an ____ on the ____
If you want to keep the same number of characters, then you can iterate over the matches:
let sentence = "This is table. There is \"an\" apple on \"the\" table."
let regularExpression = try! NSRegularExpression(pattern: "\"[^\"]+\"", options: [])
let matches = regularExpression.matches(
in: sentence,
options: [],
range: NSMakeRange(0, sentence.characters.count)
)
var newSentence = sentence
for match in matches {
let replacement = Array(repeating: "_", count: match.range.length - 2).joined()
newSentence = (newSentence as NSString).replacingCharacters(in: match.range, with: "\"" + replacement + "\"")
}
print(newSentence) // This is table. There is "__" apple on "___" table.
I wrote an extension to do this:
extension String {
mutating func replace(from: String, to: String, by new: String) {
guard let from = range(of: from)?.lowerBound, let to = range(of: to)?.upperBound else { return }
let range = from..<to
self = replacingCharacters(in: range, with: new)
}
func replaced(from: String, to: String, by new: String) -> String {
guard let from = range(of: from)?.lowerBound, let to = range(of: to)?.upperBound else { return self }
let range = from..<to
return replacingCharacters(in: range, with: new)
}
}

Find String from String using NSRegularExpression Swift

I want to fetch url of images from the String using NSRegularExpression.
func findURlUsingExpression(urlString: String){
do{
let expression = try NSRegularExpression(pattern: "\\b(http|https)\\S*(jpg|png)\\b", options: NSRegularExpressionOptions.CaseInsensitive)
let arrMatches = expression.matchesInString(urlString, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, urlString.characters.count))
for match in arrMatches{
let matchText = urlString.substringWithRange(Range(urlString.startIndex.advancedBy(match.range.location) ..< urlString.startIndex.advancedBy(match.range.location + match.range.length)))
print(matchText)
}
}catch let error as NSError{
print(error.localizedDescription)
}
}
It works with just the simple string but not with the HTML String.
Working Example:
let tempString = "jhgsfjhgsfhjgajshfgjahksfgjhs http://jhsgdfjhjhggajhdgsf.jpg jahsfgh asdf ajsdghf http://jhsgdfjhjhggajhdgsf.png"
findURlUsingExpression(tempString)
Output:
http://jhsgdfjhjhggajhdgsf.jpg
http://jhsgdfjhjhggajhdgsf.png
But not working with this one: http://www.writeurl.com/text/478sqami3ukuug0r0bdb/i3r86zlza211xpwkdf2m
Don't roll your own regex if you can help it. Easiest and safest way is to use NSDataDetector. By using NSDataDetector you leverage a pre-built, heavily used parsing tool which should already have most of the bugs shaken out of it.
Here is a good article on it: NSData​Detector
NSDataDetector is a subclass of NSRegularExpression, but instead of
matching on an ICU pattern, it detects semi-structured information:
dates, addresses, links, phone numbers and transit information.
import Foundation
let tempString = "jhgsfjhgsfhjgajshfgjahksfgjhs http://example.com/jhsgdfjhjhggajhdgsf.jpg jahsfgh asdf ajsdghf http://example.com/jhsgdfjhjhggajhdgsf.png"
let types: NSTextCheckingType = [.Link]
let detector = try? NSDataDetector(types: types.rawValue)
detector?.enumerateMatchesInString(tempString, options: [], range: NSMakeRange(0, (tempString as NSString).length)) { (result, flags, _) in
if let result = result?.URL {
print(result)
}
}
// => "http://example.com/jhsgdfjhjhggajhdgsf.jpg"
// => "http://example.com/jhsgdfjhjhggajhdgsf.png"
The example is from that site, adapted to search for a link.