Code doesn't work on Playground

Hello,

I'm developing an app that use Regular Expression and i'm with a problem.


var telefone = "+42 43 23123-2221" 
let range = telefone.rangeOfString("\\d{4,5}\\-?\\d{4}", options:.RegularExpressionSearch) 
print("range \(range)") //here returns nil


This code works on swiftstub


http://swiftstub.com/464635533/


but it doesn't work on Playground.


http://imgur.com/EKSnLyS


Thanks!!

Accepted Reply

i've changed the algorithm but it only crash on playground

It’s not just playgrounds. I pasted your code into a new Swift command line tool project and it crashed there as well. Specifically, it crashed on line 7 with

range
being nil.

It’s likely that the source of all this confusion is hyphens. In the example you posted, the hyphen in

telefone
is U+002D HYPHEN-MINUS where the hyphen in
phoneNumberRegEx
is U+2011 NON-BREAKING HYPHEN. When I standardised everything on U+002D, your code worked as you expected.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Did you import Foundation? rangeOfString is an NSString method.

At least, your code works in my envrironment and outptus this:

import UIKit

var telefone = "+42 43 23123-2221"
let range = telefone.rangeOfString("\\d{4,5}\\-?\\d{4}", options:.RegularExpressionSearch)
print("range \(range)") //here returns nil

range Optional(Range(7..<17))


Does your environment have anything special?

(Tested in the Playground of Xcode Version 7.2 (7C68).)

Yes!! i already checked it.

i'm using XCODE Version 7.2 (7C68) too. i've changed the algorithm but it only crash on playground,


http://imgur.com/ZjrlaDA



Code:


import Foundation
var telefone = "+55 11 11111‑1111"
let phoneNumberRegEx = "(?:(\\+\\d\\d\\s+)?((?:\\(\\d\\d\\)|\\d\\d)\\s+)?)(\\d{4,5}\\-?\\d{4})";
let range = telefone.rangeOfString(phoneNumberRegEx,
    options:.RegularExpressionSearch)
print("range \(range)")
var found = telefone.substringWithRange(range!)
print(found)
let regex = try! NSRegularExpression(pattern: phoneNumberRegEx, options: [])
let telephoneRange = NSMakeRange(0, telefone.characters.count)
let result = regex.firstMatchInString(telefone, options: NSMatchingOptions(rawValue: 0), range: telephoneRange)
let r1 = result!.rangeAtIndex(1)
let r2 = result!.rangeAtIndex(2)
let r3 = result!.rangeAtIndex(3)
if (r1.length > 0) {
    let phoneCountry = (telefone as NSString).substringWithRange(r1)
    print("country: \(phoneCountry)")
}
if (r2.length > 0) {
    let phoneArea = (telefone as NSString).substringWithRange(r2)
    print("area: \(phoneArea)")
}
if (r3.length > 0) {
    let phone = (telefone as NSString).substringWithRange(r3)
    print("phone: \(phone)")
}

i've changed the algorithm but it only crash on playground

It’s not just playgrounds. I pasted your code into a new Swift command line tool project and it crashed there as well. Specifically, it crashed on line 7 with

range
being nil.

It’s likely that the source of all this confusion is hyphens. In the example you posted, the hyphen in

telefone
is U+002D HYPHEN-MINUS where the hyphen in
phoneNumberRegEx
is U+2011 NON-BREAKING HYPHEN. When I standardised everything on U+002D, your code worked as you expected.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you! I had not noticed this little detail . Now it's working!!