2

How do you get an array of string values for the text between parentheses in Swift?

For example from: MyFileName(2015)(Type)(createdBy).zip

I would like: [2015,Type,createdBy]

Kevin Mann
  • 721
  • 1
  • 7
  • 14
  • 3
    I believe the term you're looking for is "parentheses". ;-) – Joshua Nozzi Nov 13 '15 at 12:45
  • 1
    @Thomas: You should post your comment as an answer. Bonus points if you elaborate it with a Swift / Cocoa regex example. :-) – Joshua Nozzi Nov 13 '15 at 12:51
  • if I use that Reg Ex in Swift I get a compile error, say from the answer in the swift 2 link by @ConradoCostal I use the function suggested and then try `let matches = matchesForRegexInText("\((.*?)\)", text: string)` I get expected separator error after the . and if I try to do this: `let regEx = "\((.*?)\)"` I get invalid character in source error. – Kevin Mann Nov 13 '15 at 14:20
  • @KevinMann This regex string conflicts with Swift's syntactic sugar for String interpolation. You have to escape each \ character with another \ to make it work. – Eric Aya Nov 13 '15 at 17:05

4 Answers4

4

Here is a complete example in Swift 4.2

func matchesForRegexInText(regex: String!, text: String!) -> [String] {

do {

    let regex = try NSRegularExpression(pattern: regex, options: [])
    let nsString = text as NSString

    let results = regex.matches(in: text,
                                        options: [], range: NSMakeRange(0, nsString.length))
    return results.map { nsString.substring(with: $0.range)}

} catch let error as NSError {

    print("invalid regex: \(error.localizedDescription)")

    return []
}}

and usage :

let regex = "\\((.*?)\\)"
mmatchesForRegexInText(regex: regex, text: " exmaple (android) of (qwe123) text (heart) between parentheses")
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
KarimIhab
  • 173
  • 1
  • 8
4

Just updating the chosen answer to Swift 3:

func matchesForRegexInText(regex: String!, text: String!) -> [String] {

do {

    let regex = try NSRegularExpression(pattern: regex, options: [])
    let nsString = text as NSString

    let results = regex.matches(in: text,
                                        options: [], range: NSMakeRange(0, nsString.length))
    return results.map { nsString.substring(with: $0.range)}

} catch let error as NSError {

    print("invalid regex: \(error.localizedDescription)")

    return []
}}

The usage remains the same.

Dimas Mendes
  • 2,664
  • 1
  • 21
  • 19
0

You can use a regex for this

Thomas had a good example: \((.*?)\)

How to use a regex with Swift you can look up at: http://www.raywenderlich.com/86205/nsregularexpression-swift-tutorial

Community
  • 1
  • 1
siegy22
  • 4,295
  • 3
  • 25
  • 43
0

Here is my RegEx which is actually trying to get the words between parentheses. E.g. (smile)

NSRegularExpression(pattern: "\\\\(\\\w+\\\\)",options:NSRegularExpressionOptions.CaseInsensitive)

it works for me!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
karthik
  • 621
  • 4
  • 14