Specifically in Python 3.3 and above, is it sufficient to check for orphan surrogates by using the simple match:
re.search(r'[\uD800-\uDFFF]', s)
Based on the assumption that all legal surrogates would have been represented as astral code points and thus would not match, leaving out the illegal surrogates, or is there caveats and edge cases one needs to be aware of?
Yes, that's correct. Code units 0xD800–0xDFFF don't represent valid characters in wide Unicode strings, and in Python 3.3+ (following PEP 393) all Unicode strings are effectively wide.
来源:https://stackoverflow.com/questions/32563944/checking-for-illegal-surrogates-in-python-3-strings