0

How would I extract the RBG value from the following string?:

let string = "<span style=\"background-color: rgb(230, 0, 0);\">

I would like to get extract "rgb(230, 0, 0)" from the background-color value, so I can convert it to a hex string and update some UI.

let range = string.range(of: "\"background-color: ")
let startPoint = range.location + range.length
let subString = string.substring(from: startPoint)
//generating error claiming 'cannot convert value of type 'int' to expected argument type 'string.index'. 
//paused here to ask S.O b/c I do not think substrings is the best way to do this, maybe there is a library that extracts values from html elements, or regex?
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
  • https://github.com/100mango/SwiftCssParser seems useful – Alexander Dec 05 '17 at 19:29
  • 1
    -1, and voting to close... Seriously, as a «lead iOS developer» (based on your profile), you should at least try something and ask specific questions... «Plz give me the code» is not acceptable here. – Macmade Dec 05 '17 at 19:56
  • 1
    Moreover, why would you want a hex value, when the built in initializer for `UIColor` accepts RGBA values, not hex. – Dávid Pásztor Dec 05 '17 at 19:59
  • 2
    Wow guys!! the flame is on super high today! Thanks to Leo for supplying the answer for me. I was struggling for ~20 minutes to get my foot off the ground, hence the lack of code until my answer. I dont think S.O is a place to talk smack on each other. We are all in this to help each other out! – Josh O'Connor Dec 05 '17 at 20:02
  • 1
    You're right... But then at least please show some effort... – Macmade Dec 05 '17 at 20:05
  • I'm downvoting, because in my opinion there is a lack of tries. Not because your profile says you are a iOS Lead Developer, I clearly don't care about that, but because you have enough reputation to know what a good question should looks like. Some tries with a regex (maybe showing one not working), some tries with `rangeOf`, etc. An explanation on your context, is the string always looking like this, do you have other variants (not a `background-color`, but other possible attributes), etc. – Larme Dec 05 '17 at 20:31
  • Alrighty then, Ill update my question with my originial try, which was incredibly wrong – Josh O'Connor Dec 05 '17 at 20:32
  • Even if I still find it of poor quality (error is explicit, but that's my experience talking and since I do not take in account your coding skills), I consider it not that bad enough to keep my downvote, so I remove it. – Larme Dec 05 '17 at 20:40

2 Answers2

2

You can use a regex to get the string between two strings:

let string = "<span style=\"background-color: rgb(230, 0, 0);\">"

let pattern = "(?<=background-color: )(.*)(?=;)"
if let rgb = string.range(of: pattern, options: .regularExpression).map({String(string[$0])}) {
    print(rgb)  // "rgb(230, 0, 0)"
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0
let range = (string as NSString).range(of: "\"background-color: ")
let startPoint = range.location + range.length
let subString = (string as NSString).substring(from: startPoint)
let deliminator = ";"
let components = subString.components(separatedBy: deliminator)
let rgb = components[0]

print(rgb) -> "rgb(230, 0, 0)"
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
  • Your answer won't work with the string in your question. The string in your question does not contain the string `"color: rgb(`. And why are you using `NSString` in Swift? – rmaddy Dec 05 '17 at 19:50
  • And your question states you want to extract `rgb(230, 0, 0)` but this answer only extracts `230, 0, 0`. – rmaddy Dec 05 '17 at 19:53
  • Im using NSString b/c NSString substring(from: ) method accepts an int but the String subString(from: ) accepts a string.index. And yes, I updated my answer to extract rgb(230, 0, 0) instead of the digits, sorry. – Josh O'Connor Dec 05 '17 at 20:01
  • If you use just `String` methods, you will get ranges that work with `String`. There is no need to use `NSString`. – rmaddy Dec 05 '17 at 20:27
  • well, this is not the correct to handle this, as regex is much more graceful, but got your point – Josh O'Connor Dec 05 '17 at 20:36