1

I have a string as shown below and i am trying to separate each part (,) into an array.

let stringArr = remainderString.componentsSeparatedByString(",")

0lY6P5Ur90TAQnnnI6wtnb,#29,Projekt-FZK-Haus,Projekt FZK-House create by KHH Forschuungszentrum Karlsruhe,$,$,$,(#67,#229,#275),#42

Results

["0lY6P5Ur90TAQnnnI6wtnb", "#29", "Projekt-FZK-Haus", "Projekt FZK-House create by KHH Forschuungszentrum Karlsruhe", "$", "$", "$", "(#67", "#229", "#275)", "#42"]

If you notice, the part (#67,#229,#275) were separated into "(#67", "#229", "#275)"

I want to insert those values inside the (braces) into an another array. So my question is, how can I locate the opening ( and then the closing ) ?

3
  • one question, you want to get all (#67,#229,#275) as one text, or you want only to know when open an closing your parenthesis? Commented Jul 7, 2016 at 21:20
  • 1
    You could use a let regex = "\\([^()]*\\)|[^,]+" regex to match the substrings separately. Commented Jul 7, 2016 at 21:24
  • @ReinierMelian I am trying to match an opening brace with a closing brace. If i get it as one text, then that is fine. Commented Jul 7, 2016 at 21:29

2 Answers 2

2

Try this:

// Subscripting a String with NSRange. Make dealing with ObjC-classes easier
extension String {
    subscript(range: NSRange) -> String {
        let startIndex = self.startIndex.advancedBy(range.location)
        let endIndex = startIndex.advancedBy(range.length)

        return self[startIndex..<endIndex]
    }
}

let str = "0lY6P5Ur90TAQnnnI6wtnb,#29,Projekt-FZK-Haus,Projekt FZK-House create by KHH Forschuungszentrum Karlsruhe,$,$,$,(#67,#229,#275),#42"
let regex = try! NSRegularExpression(pattern: "\\((#[^\\)]+)\\)", options: [])

if let match = regex.firstMatchInString(str, options: [], range: NSMakeRange(0, str.characters.count)) {
    let substr = str[match.rangeAtIndex(1)]
    let components = substr.componentsSeparatedByString(",")

    print(components)
}
Sign up to request clarification or add additional context in comments.

2 Comments

To work with ranges based on NSString's nature, you cannot use characters based index. NSString uses UTF-16 based index, so your code needs to be modified to use utf16-based index and range.
@Code Different thanks a lot. I did and it seems to work.
0

Another example. Using UTF-16 based index and range properly, so works with Strings including non-BMP characters.

let str = "0lY6P5Ur90TAQnnnI6wtnb,#29,Projekt-FZK-Haus,Projekt FZK-House create by KHH Forschuungszentrum Karlsruhe,$,$,$,(#67,#229,#275),#42"
let pattern = "(\\([^\\)]*\\)|[^\\(,][^,]*)(?:,|$)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matchesInString(str, options: [], range: NSRange(0..<str.utf16.count))
let stringArr = matches.map{match->String in
    let matchingRange = match.rangeAtIndex(1)
    let matchingString = (str as NSString).substringWithRange(matchingRange) as String
    return matchingString
}
print(stringArr) //->["0lY6P5Ur90TAQnnnI6wtnb", "#29", "Projekt-FZK-Haus", "Projekt FZK-House create by KHH Forschuungszentrum Karlsruhe", "$", "$", "$", "(#67,#229,#275)", "#42"]

1 Comment

Exactly the solution I was looking for. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.