2.7. Unicode Code Points, Properties, Blocks, and Scripts
Problem
Use a regular expression to find the trademark sign (™) by specifying its Unicode code point rather than copying and pasting an actual trademark sign. If you like copy and paste, the trademark sign is just another literal character, even though you cannot type it directly on your keyboard. Literal characters are discussed in Recipe 2.1.
Create a regular expression that matches any character that has the “Currency Symbol” Unicode property. Unicode properties are also called Unicode categories.
Create a regular expression that matches any character in the “Greek Extended” Unicode block.
Create a regular expression that matches any character that, according to the Unicode standard, is part of the Greek script.
Create a regular expression that matches a grapheme, or what is commonly thought of as a character: a base character with all its combining marks.
Solution
Unicode code point
\u2122
Regex options: None |
Regex flavors: .NET, Java, JavaScript, Python |
This regex works in Python only when quoted as a Unicode
string: u"\u2122"
.
\x{2122}
Regex options: None |
Regex flavors: PCRE, Perl, Ruby 1.9 |
PCRE must be compiled with UTF-8 support; in PHP, turn on
UTF-8 support with the /u
pattern
modifier. Ruby 1.8 does not support Unicode regular
expressions.
Unicode property or category
\p{Sc}
Regex options: None |
Regex flavors: .NET, Java, PCRE, Perl, Ruby 1.9 |
PCRE must be compiled with UTF-8 support; in PHP, turn on
UTF-8 support with the /u
pattern ...
Get Regular Expressions Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.