Fix Unicode regex patterns to support international characters #1476
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Unicode Regex Patterns to Support International Characters
Fixes #1420
Summary
[a-zA-Z0-9_]with Unicode character classes[\\p{L}\\p{M}\\p{N}_]in both highlightPattern and autoCompletePattern regex patternsmastodon-ios/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Helper/MastodonRegex.swift
Line 31 in 19b6947
Problem
The current regex patterns in MastodonRegex.swift were using ASCII-only character classes
[a-zA-Z0-9_]which don't properly match international Unicode characters. This meant that usernames and domains containing characters with combining marks (like café or examplë.com) or other Unicode characters (like Chinese or Japanese characters) would be cut off prematurely, as these characters were not being recognized as valid parts of the username or domain.Solution
Updated both regex patterns to use comprehensive Unicode character classes:
highlightPattern: Changed from@([a-zA-Z0-9_]+)to@([\\p{L}\\p{M}\\p{N}_]+)autoCompletePattern: Changed from@([a-zA-Z0-9_]+)to@([\\p{L}\\p{M}\\p{N}_]+)The new character classes include:
\\p{L}- Unicode letters from any language\\p{M}- Combining marks (accents, umlauts, etc.)\\p{N}- Unicode numbers_- Underscore (preserved from original pattern)This change allows the regex to properly recognize a much broader range of characters as valid parts of usernames and domains, including but not limited to:
Tests
Added comprehensive test suite in MastodonRegexTests.swift with test cases including:
@café@examplë.com(characters with combining marks)@unikonstanz@bawü.social(German umlauts)@user@täst.de(another example with umlauts)@björn@exämple.com(multiple combining marks)@用户@example.com(Chinese characters)@example@例え.テスト(Japanese characters)All tests verify that the regex patterns correctly match the full username and domain, including all Unicode characters.