Reputation: 193
Here I try to find a way to get the second word of my array and created a new list up with the words retrieve
Here are the array
[Orange, Orange Ekstraklasa, Orange (entreprise), Orange (Vaucluse), Orange mécanique, Orange-Nassau, Orange (fruit), Orange Cinémax, Orange TV, Orange Stinger]
Here is an example of the word that I want to recover
etc...
I think I have created a regular expression and a space for parthentèse recovers the second word.
I find this method to match my expression and I ask if there was another one that creates a new array with the word select
here is the method
let myStringToBeMatched = "ThisIsMyString"
let myRegex = "^This"
if let match = myStringToBeMatched.rangeOfString(myRegex, options: .RegularExpressionSearch){
println("\(myStringToBeMatched) is matching!")
}
I tried the solution this solution works well outside the alamofire method:
let list = ["Orange", "Orange Ekstraklasa", "Orange (entreprise)", "Orange (Vaucluse)", "Orange mécanique", "Orange-Nassau", "Orange (fruit)", "Orange Cinémax", "Orange TV", "Orange Stinger"]
let results = list.map { split($0, {$0 == " " || $0 == "(" || $0 == ")"}) }
.filter { $0.count > 1 }
.map { $0[1] }
So I adapted to functioning in alamofire this
Alamofire.request(.GET, "http://fr.wikipedia.org/w/api.php", parameters: parameters)
.responseJSON { (_, _, data, _) in
let json = JSON(object: data!)
var list = json[1].arrayValue!
let results = list.map { split($0, {$0 == " " || $0 == "(" || $0 == ")"}) }
.filter { $0.count > 1 }
.map { $0[1] }
}
at my variable results
it myself but as error: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
Thanks
Upvotes: 0
Views: 613
Reputation: 437381
In addition to regex, you could also do something like:
let list = ["Orange", "Orange Ekstraklasa", "Orange (entreprise)", "Orange (Vaucluse)", "Orange mécanique", "Orange-Nassau", "Orange (fruit)", "Orange Cinémax", "Orange TV", "Orange Stinger"]
let results = list.map { split($0, {$0 == " " || $0 == "(" || $0 == ")"}) }
.filter { $0.count > 1 }
.map { $0[1] }
That builds an array of words for each item in the original list, filters out those with less than 2 items, and then builds new array of the second items.
A little less elegant, but probably more efficient, would be
let results = list
.map {
(string) -> String! in
let words = split(string, {$0 == " " || $0 == "(" || $0 == ")"})
if words.count > 1 {
return words[1]
} else {
return nil
}
}
.filter { $0 != nil }
That builds array of the second words (or nil
if there was only one word) and then filters out the nil
values.
The regex equivalent would be:
var error: NSError?
let regex = NSRegularExpression(pattern: "[\\w-]+", options: nil, error: &error)
let results = list
.map {
(string) -> String! in
let matches = regex?.matchesInString(string, options: nil, range: NSMakeRange(0, countElements(string))) as [NSTextCheckingResult]?
if matches?.count > 1 {
return (string as NSString).substringWithRange(matches![1].range)
} else {
return nil
}
}
.filter { $0 != nil }
If you'd prefer traditional for
loops:
var results = [String]()
for string in list {
let words = split(string, {$0 == " " || $0 == "(" || $0 == ")"})
if words.count > 1 {
results.append(words[1])
}
}
or, its regex equivalent:
var error: NSError?
let regex = NSRegularExpression(pattern: "[\\w-]+", options: nil, error: &error)
var results = [String]()
for string in list {
let matches = regex?.matchesInString(string, options: nil, range: NSMakeRange(0, countElements(string))) as [NSTextCheckingResult]?
if matches?.count > 1 {
results.append((string as NSString).substringWithRange(matches![1].range))
}
}
Upvotes: 1
Reputation: 193
do not understand why he told me this line that I get an error like:
Could not find member 'stringValue'
the error is at the println ()
if let match = json[1][index].stringValue?.rangeOfString(myRegex, options: .RegularExpressionSearch) {
println(json[1][match].stringValue?)
}
Here is the rest of the code
let json = JSON(object: data!)
let list: Array<JSON> = json[1].arrayValue!
var index:Int
for(index = 0 ; index < list.count ; index++) {
let myRegex = "\\((.*?)\\)"
if let match = json[1][index].stringValue?.rangeOfString(myRegex, options: .RegularExpressionSearch){
println(json[1][match].stringValue?)
}
}
Upvotes: 0
Reputation: 8927
The rangeOfString returns a range, you can use it to create a new string instance.
let myStringToBeMatched = "ThisIsMyString"
let myRegex = "^This"
if let match = myStringToBeMatched.rangeOfString(myRegex, options: .RegularExpressionSearch){
let result = myStringToBeMatched[match]
println("\(result) is matching!")
}
Upvotes: 0