NSRegularExpressions will get stuck when matching strings containing emoji with String.count.

Xcode 13.2.1 Swift 5

This problem occurs on iOS simulator and real device (both with iOS 15.2).

It seems to occur only when dotMatchesLineSeparators is specified as an option. When the same code is implemented in Objective-C, the problem does not occur.

This will not occur if the length of range is set to message.utf16.count.

let message = "Hello 😂";

let regexp = try! NSRegularExpression(pattern: "((.*\n){15})", options: .dotMatchesLineSeparators)

let res = regexp.rangeOfFirstMatch(
    in: message,
    options: [],
    range: NSRange(location: 0, length: message.count)
).location != NSNotFound

print(res)

Isn't it a bug in NSRegularExpression?

I have same issue.

Using utf16.count did not cause a hang. The problem did not occur when the emoji was not at the end.

ex)

let message = "Hello 😂 A";

I believe the difference between message.count and message.utf16.count has something to do with the hang of the symbol with the emoji data split at the end of the string.

However, I don't know why this problem occurs.

NSRegularExpressions will get stuck when matching strings containing emoji with String.count.
 
 
Q