I am trying to match a regex pattern in a string in Swift. When I use the actual characters in regex pattern, it works as expected. However, I use Unicode versions of the same characters in regex, it does not work as expected. Could you please help me with what is wrong here. I need to use regex with Unicode.
Code:
var input = "一" // u{4E00} extension String { var patternMatchesWithUnicode: Bool { //doesnt work return self.range(of: #"[\u{4E00}-\u{9FFF}]"#, options: .regularExpression) != nil } var patternMatchesWithString: Bool { //works return self.range(of: #"[一-鿿]"#, options: .regularExpression) != nil } } print(input.patternMatchesWithString) print(input.patternMatchesWithUnicode) Output:
false true