teclis teclis - 1 year ago 418
Swift Question

Search for a Regular Expression in Swift 3

At the moment I'm converting my project to Swift 3.

I have a code block like this:

let someString = "asd.asABCDEFG.HI"
let regexp = "^\\w*[.]\\w{2}"
let range = someString.rangeOfString(regexp, options: .RegularExpressionSearch)
let result = someString.substringWithRange(range!)

method is gone in Swift 3. Can somebody post an example, how a regexp search can be done. Thanks in advance.

Answer Source

In Swift 3 rangeOfString is like range(of:options:) and substringWithRange is like substring(with:).

if let range = someString.range(of:regexp, options: .regularExpression) {
    let result = someString.substring(with:range)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download