45

I am attempting to use regular expression to replace all occurrences of UK car registrations within a string.

The following swift code works perfectly for a when the string matches the regex exactly as below.

var myString = "DD11 AAA"
var stringlength = countElements(myString) 
var ierror: NSError?
var regex:NSRegularExpression = NSRegularExpression(pattern: "^([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}$", options: NSRegularExpressionOptions.CaseInsensitive, error: &ierror)!
var modString = regex.stringByReplacingMatchesInString(myString, options: nil, range: NSMakeRange(0, stringlength), withTemplate: "XX")
print(modString)

The result is XX

However, the following does not work and the string is not modifed

var myString = "my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB"
var stringlength = countElements(myString) 
var ierror: NSError?
var regex:NSRegularExpression = NSRegularExpression(pattern: "^([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}$", options: NSRegularExpressionOptions.CaseInsensitive, error: &ierror)!
var modString = regex.stringByReplacingMatchesInString(myString, options: nil, range: NSMakeRange(0, stringlength), withTemplate: "XX")
print(modString)

The result is my car reg 1 - DD11 AAA my car reg 2 - AA22 BBB

Can anyone give me any pointers?

Jérôme
  • 8,016
  • 4
  • 29
  • 35
jjc99
  • 3,559
  • 4
  • 22
  • 21

8 Answers8

54

You need to remove the ^ and $ anchors.

The ^ means start of string and $ means end of string (or line, depending on the options). That's why your first example works: in the first test string, the start of the string is really followed by your pattern and ends with it.

In the second test string, the pattern is found in the middle of the string, thus the ^... can't apply. If you would just remove the ^, the $ would apply on the second occurrence of the registration number and the output would be my car reg 1 - DD11 AAA my car reg 2 - XX.

let myString = "my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB"
let regex = try! NSRegularExpression(pattern: "([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}", options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, myString.count)
let modString = regex.stringByReplacingMatches(in: myString, options: [], range: range, withTemplate: "XX")
print(modString)
// Output: "my car reg 1 - XX  my car reg 2 - XX"
bjornte
  • 749
  • 1
  • 9
  • 31
DarkDust
  • 90,870
  • 19
  • 190
  • 224
32

Let's use a class extension to wrap this up in Swift 3 syntax:

extension String {
    mutating func removingRegexMatches(pattern: String, replaceWith: String = "") {
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
            let range = NSRange(location: 0, length: count)
            self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
        } catch { return }
    }
}

var phoneNumber = "+1 07777777777"
phoneNumber.removingRegexMatches(pattern: "\\+\\d{1,4} (0)?")

Results in 7777777777 (thus removing country code from phone number)

Peter
  • 2,654
  • 2
  • 33
  • 44
Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57
  • use of NSMakeRange generates `warning: Legacy Constructor Violation: Swift constructors are preferred over legacy convenience functions. (legacy_constructor)` in swiftlint – ablarg Jan 05 '18 at 20:48
  • @ablarg you can fix that by replacing the line with: `let range = NSRange(location: 0, length: self.count)` – Constantine May 15 '20 at 08:20
11

Swift 4.2 Updated

let myString = "my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB"
if let regex = try? NSRegularExpression(pattern: "([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}", options: .caseInsensitive) {
    let modString = regex.stringByReplacingMatches(in: myString, options: [], range: NSRange(location: 0, length:  myString.count), withTemplate: "XX")
    print(modString)
}
black_pearl
  • 2,549
  • 1
  • 23
  • 36
9

Update for Swift 2.1:

var myString = "my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB"
if let regex = try? NSRegularExpression(pattern: "([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}", options: .CaseInsensitive) {
    let modString = regex.stringByReplacingMatchesInString(myString, options: .WithTransparentBounds, range: NSMakeRange(0, myString.characters.count), withTemplate: "XX")
    print(modString)
}
Daniel J
  • 446
  • 6
  • 8
7

Warning

Do not use NSRange(location: 0, length: myString.count) as all examples above quoted.

Use NSRange(myString.startIndex..., in: myString) instead!

.count will count newline characters like \r\n as one character - this may result in a shortened, thus invalid, NSRange that does not match the whole string.

(.length should work)

coyer
  • 4,122
  • 3
  • 28
  • 35
6

Simple extension:

extension String {

    func replacingRegex(
        matching pattern: String,
        findingOptions: NSRegularExpression.Options = .caseInsensitive,
        replacingOptions: NSRegularExpression.MatchingOptions = [],
        with template: String
    ) throws -> String {

        let regex = try NSRegularExpression(pattern: pattern, options: findingOptions)
        let range = NSRange(startIndex..., in: self)
        return regex.stringByReplacingMatches(in: self, options: replacingOptions, range: range, withTemplate: template)
    }
 }

✅ Advantages to other answers

  • Exposed throwing error to the caller
  • Exposed finding options to the caller with default for the ease of use
  • Exposed replacing options to the caller with default for the ease of use
  • Fixed the range BUG in the original answer
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
4

With pattern: "^ ... $" you have specified that the pattern is anchored to the start and end of the string, in other words, the entire string must match the pattern. Just remove ^ and $ from the pattern and you'll get the expected result.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
3

A notice to all answers that uses .count in their answers:

This will cause problems in cases that the operating target range has surrogate-paired characters.

Please fix your answers by using .utf16.count instead.

Here's Ryan Brodie 's answer with this fix. It works with Swift 5.5.

private extension String {
    mutating func regReplace(pattern: String, replaceWith: String = "") {
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines])
            let range = NSRange(location: 0, length: self.utf16.count)
            self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
        } catch { return }
    }
}

Update: If considering @coyer 's concerns:

private extension String {
    mutating func regReplace(pattern: String, replaceWith: String = "") {
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines])
            let range = NSRange(self.startIndex..., in: self)
            self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
        } catch { return }
    }
}

Also: to @Martin R' : It is okay to use ^ and $ in Regex as long as you have enabled the ".anchorsMatchLines" in the Regex options. I already applied this option in the codeblocks above.

Shiki Suen
  • 67
  • 1
  • 9