Swift Regex matching fails when source contains Unicode characters

I am trying to make a simple regular expression using NSRegularExpression, but I am having some problems matching the string when the source contains multibyte characters:

let string = "D 9"

// The following matches (any characters)(SPACE)(numbers)(any characters)
let pattern = "([\\s\\S]*) ([0-9]*)(.*)"

let slen : Int = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

var error: NSError? = nil

var regex = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: &error)

var result = regex?.stringByReplacingMatchesInString(string, options: nil, range: NSRange(location:0,
length:slen), withTemplate: "First \"$1\" Second: \"$2\"")

The above code returns "D" and "9" as expected

If you now change the first line to include the UK currency symbol "pound sterling" as follows:

let string = "£ 9"

Then the match does not work, although part of the expression ([\\s\\S]*)must still match any leading characters.

I understand that a character £will take two bytes, but pattern matching should be ignored, right?

Can anyone explain what is going on here?

+4
1

. stringByReplacingMatchesInString() NSString Objective-C String Swift, range: - a NSRange. NSString ( UTF-16):

var result = regex?.stringByReplacingMatchesInString(string,
        options: nil,
        range: NSRange(location:0, length:(string as NSString).length),
        withTemplate: "First \"$1\" Second: \"$2\"")

count(string.utf16) (string as NSString).length.

:

let string = "£ 9"

let pattern = "([\\s\\S]*) ([0-9]*)(.*)"
var error: NSError? = nil
let regex = NSRegularExpression(pattern: pattern,
        options: NSRegularExpressionOptions.DotMatchesLineSeparators,
        error: &error)!

let result = regex.stringByReplacingMatchesInString(string,
    options: nil,
    range: NSRange(location:0, length:(string as NSString).length),
    withTemplate: "First \"$1\" Second: \"$2\"")
println(result)
// First "£" Second: "9"
+8

All Articles