find emojis in a string using regex in swift

To find emojis in a string using regex in Swift, you can use the following regular expression pattern:

main.swift
let emojiPattern = #"[^\p{C}\p{Z}\p{P}\p{S}]{1,2}"#
52 chars
2 lines

This pattern matches any Unicode character that is not a control character (\p{C}), a whitespace character (\p{Z}), a punctuation character (\p{P}), or a symbol character (\p{S}). The 1,2 quantifier ensures that only valid emojis of length 1 or 2 are matched.

Then, you can use the NSRegularExpression class to search for all matches of the pattern in your string:

main.swift
let regex = try! NSRegularExpression(pattern: emojiPattern)
let string = "I ā¤ļø Swift šŸŽ‰šŸŽˆ"
let range = NSRange(location: 0, length: string.utf16.count)

let matches = regex.matches(in: string, range: range)
for match in matches {
    let emoji = (string as NSString).substring(with: match.range)
    print(emoji)
}
315 chars
10 lines

This will output:

main.swift
ā¤ļø
šŸŽ‰
šŸŽˆ
9 chars
4 lines

Note that some emojis may be represented by multiple Unicode characters, so you may want to normalize the string to ensure that all emojis are represented consistently. You can use the String method precomposedStringWithCanonicalMapping for this:

main.swift
let normalizedString = string.precomposedStringWithCanonicalMapping
let normalizedMatches = regex.matches(in: normalizedString, range: range)
for match in normalizedMatches {
    let emoji = (normalizedString as NSString).substring(with: match.range)
    print(emoji)
}
270 chars
7 lines

This will output the same result as before.

gistlibby LogSnag