How can I use NSRegularExpression on Swift strings

2019-04-06 00:22发布

问题:

I'm having trouble getting NSRegularExpression to match patterns on strings with wider (?) Unicode characters in them. It looks like the problem is the range parameter -- Swift counts individual Unicode characters, while Objective-C treats strings as if they're made up of UTF-16 code units.

Here is my test string and two regular expressions:

let str = "dog