# NSRegularExpression Tutorial: Getting Started

**URL:** <https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 4, 2016, 10:51am UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561 "2016-03-04T10:51:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![frosty](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/frosty/32/69715_2.png) [@frosty](https://forums.kodeco.com/u/frosty)\
**Post date:** [March 4, 2016, 10:51am UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/1 "2016-03-04T10:51:27Z")

</div>

Learn how to search, replace, and validate data in your app with this NSRegularExpression tutorial. Includes a handy cheat sheet and playground for you to take home!

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/2018-nsregularexpression-tutorial-getting-started](https://www.raywenderlich.com/2018-nsregularexpression-tutorial-getting-started)

---

<div class="post-metadata">

**Author:** ![aestus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aestus/32/81494_2.png) [@aestus](https://forums.kodeco.com/u/aestus)\
**Post date:** [April 14, 2016, 2:18am UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/2 "2016-04-14T02:18:16Z")

</div>

I would greatly appreciate if this was updated to Swift 2.0. Please and thanks. (Seriously, I would pay like 10$ to get this updated to Swift 2)

EDIT: OK I have figured out the updated code for the Search and Replace Sections (As well as the highlighting text part) If the article writer or whoever wants to update the article let me know but here is the code:

When you create the RegexHelpers.swift file, this is the code:

extension NSRegularExpression {  
convenience init?(options: SearchOptions) {  
let searchString = options.searchString  
let isCaseSensitive = options.matchCase  
let isWholeWords = options.wholeWords  
let regexOption: NSRegularExpressionOptions = (isCaseSensitive) ? [] : .CaseInsensitive  
let pattern = (isWholeWords) ? “\b(searchString)\b” : searchString

```
    do {
     try self.init(pattern: pattern, options: regexOption)
    } catch {
        print(error)
    }
}

```

}

For the searchForText function:

func searchForText(searchText: String, replaceWith replacementText: String, inTextView textView: UITextView) {

```
    let beforeText = textView.text
    let range = NSMakeRange(0, beforeText.characters.count)
    
    if let regex = NSRegularExpression(options: self.searchOptions!) {
        let afterText = regex.stringByReplacingMatchesInString(beforeText, options: [], range: range, withTemplate: replacementText)
        
        textView.text = afterText
    }
}

```

For the highlighting function

func highlightText(searchText: String, inTextView textView: UITextView) {  
let attributedText = textView.attributedText.mutableCopy() as! NSMutableAttributedString

```
    let attributedTextRange = NSMakeRange(0, attributedText.length)
    
    if let regex = NSRegularExpression(options: self.searchOptions!){
        let range = NSMakeRange(0, (textView.text.characters.count))
        let matches = regex.matchesInString(textView.text, options: [], range: range)
        
        for match in matches as! [NSTextCheckingResult] {
            let matchRange = match.range
            
            attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellowColor(), range: matchRange)
        }
    }
    textView.attributedText = attributedText.copy() as! NSAttributedString
    
}

```

---

<div class="post-metadata">

**Author:** ![aestus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aestus/32/81494_2.png) [@aestus](https://forums.kodeco.com/u/aestus)\
**Post date:** [April 14, 2016, 6:08pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/3 "2016-04-14T18:08:09Z")

</div>

For anyone doing this with Swift 2. You will get an error when you launch the provided projects for lines with “.allZeroes”. (It’ll make sense if you look at the code.) Just replace it with “[]” (empty brackets, no quotes). Here is the page I found this on: [Problem with swift 2.0 and .allZer… | Apple Developer Forums](https://forums.developer.apple.com/thread/7408)

---

<div class="post-metadata">

**Author:** ![tel](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@tel](https://forums.kodeco.com/u/tel)\
**Post date:** [May 12, 2016, 7:17pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/4 "2016-05-12T19:17:45Z")

</div>

I also hope this tutorial will be updated to Swift 2. Also would be great to have a more basic tutorial on Regex, I found this very hard to follow and understand.

---

<div class="post-metadata">

**Author:** ![thierryh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/thierryh/32/81494_2.png) [@thierryh](https://forums.kodeco.com/u/thierryh)\
**Post date:** [October 29, 2016, 3:58pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/5 "2016-10-29T15:58:00Z")

</div>

I also hope this tutorial will be updated to Swift 3

---

<div class="post-metadata">

**Author:** ![gregu90](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gregu90/32/81494_2.png) [@gregu90](https://forums.kodeco.com/u/gregu90)\
**Post date:** [November 8, 2016, 3:13pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/6 "2016-11-08T15:13:13Z")

</div>

There is a mistake in Regex example in date of birth:

> “^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d$” // Date of Birth

has format MM-dd-YYYY and not dd-MM-YYYY as the task asked for

---

<div class="post-metadata">

**Author:** ![vandana](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vandana/32/48460_2.png) [@vandana](https://forums.kodeco.com/u/vandana)\
**Post date:** [November 22, 2016, 10:50am UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/7 "2016-11-22T10:50:18Z")

</div>

I also hope this tutorial will be updated to Swift 3

---

<div class="post-metadata">

**Author:** ![heavymtlhippy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/heavymtlhippy/32/68880_2.png) [@heavymtlhippy](https://forums.kodeco.com/u/heavymtlhippy)\
**Post date:** [July 19, 2017, 10:56pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/8 "2017-07-19T22:56:53Z")

</div>

This is a great tutorial. But as many have said it has not been updated to Swift 3. But considering Swift 4 will be releasing soon, I (and I’m sure many others) would appreciate an update for either/or both 3 AND 4

---

<div class="post-metadata">

**Author:** ![heavymtlhippy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/heavymtlhippy/32/68880_2.png) [@heavymtlhippy](https://forums.kodeco.com/u/heavymtlhippy)\
**Post date:** [July 20, 2017, 10:41pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/9 "2017-07-20T22:41:43Z")

</div>

For those interested I have found this resource which seems to be designed with Swift 3 in mind. [Documentation - Stack Overflow](https://stackoverflow.com/documentation/swift/5763/nsregularexpression-in-swift#t=201707202240434495651)

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 16, 2017, 10:35pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/10 "2017-11-16T22:35:11Z")

</div>

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 16, 2017, 10:35pm UTC](https://forums.kodeco.com/t/nsregularexpression-tutorial-getting-started/6561/11 "2017-11-16T22:35:13Z")

</div>

Closed on November 16, 2017, 10:35pm UTC


