Slide 1

Slide 1 text

Unicode Regular Expressions s/ / /g Nick Patch Shutterstock

Slide 2

Slide 2 text

Perl has some of the best Unicode support today, especially with respect to regular expressions. Benjamin Peterson The Guts of Unicode in Python PyCon 2013

Slide 3

Slide 3 text

UTF-8 encoded input

Slide 4

Slide 4 text

UTF-8 encoded input ⇩ decode

Slide 5

Slide 5 text

UTF-8 encoded input ⇩ decode ⇩ character string

Slide 6

Slide 6 text

UTF-8 encoded input ⇩ decode ⇩ character string ⇩ hack… hack… hack…

Slide 7

Slide 7 text

UTF-8 encoded input ⇩ decode ⇩ character string ⇩ hack… hack… hack… ⇩ encode

Slide 8

Slide 8 text

UTF-8 encoded input ⇩ decode ⇩ character string ⇩ hack… hack… hack… ⇩ encode ⇩ UTF-8 encoded output

Slide 9

Slide 9 text

use utf8;

Slide 10

Slide 10 text

use utf8; $word =~ s{ (?: دابآ | هراب | یدنب | يدنب | نیرت | یزیر | يزیر | یزاس | يزاس | ییاه ) $}{}x;

Slide 11

Slide 11 text

use utf8; $word =~ s{ (?: ия # definite articles for nouns: | ът # ∙ masculine | та # ∙ feminine | то # ∙ neutral | те # ∙ plural ) $}{}x;

Slide 12

Slide 12 text

use open qw( :encoding(UTF-8) :std );

Slide 13

Slide 13 text

use utf8; use open qw( :encoding(UTF-8) :std ); use Test::More tests => 66; use Lingua::Stem::UniNE::CS qw( stem ); is stem('zvířatech'), 'zvíř', 'rm -atech'; is stem('zvířatům'), 'zvíř', 'rm -atům'; is stem('zvířata'), 'zvíř', 'rm -ata'; is stem('zvířaty'), 'zvíř', 'rm -aty';

Slide 14

Slide 14 text

decode_json($json)

Slide 15

Slide 15 text

decode('UTF-8', $arg)

Slide 16

Slide 16 text

use v5.12;

Slide 17

Slide 17 text

use v5.14;

Slide 18

Slide 18 text

use charnames ':full';

Slide 19

Slide 19 text

use charnames ':full'; sub remove_kasra { my ($word) = @_; $word =~ s{ \N{ARABIC KASRA} $}{}x; return $word; }

Slide 20

Slide 20 text

use v5.16; sub remove_kasra { my ($word) = @_; $word =~ s{ \N{ARABIC KASRA} $}{}x; return $word; }

Slide 21

Slide 21 text

\d

Slide 22

Slide 22 text

\d 123…

Slide 23

Slide 23 text

\d 123… … ১২৩

Slide 24

Slide 24 text

\d 123… … ১২৩ … ໑໒໓

Slide 25

Slide 25 text

[0-9] 123…

Slide 26

Slide 26 text

\w

Slide 27

Slide 27 text

\w abc… 123… _

Slide 28

Slide 28 text

\w abc… 123… _ αβγ… … かきく

Slide 29

Slide 29 text

\w abc… 123… _ αβγ… … かきく …د ج ب

Slide 30

Slide 30 text

[A-Za-z0-9_] abc… 123… _

Slide 31

Slide 31 text

\b abc… 123… _ αβγ… … かきく …د ج ب

Slide 32

Slide 32 text

\s

Slide 33

Slide 33 text

\R

Slide 34

Slide 34 text

\R LF (\n) CR (\r) FF (\f)

Slide 35

Slide 35 text

\R LF (\n) CR (\r) FF (\f) CRLF (\r\n)

Slide 36

Slide 36 text

\R LF (\n) CR (\r) FF (\f) CRLF (\r\n) NEL VT LS PS

Slide 37

Slide 37 text

.

Slide 38

Slide 38 text

\X

Slide 39

Slide 39 text

\X n ̈

Slide 40

Slide 40 text

\X Spınal Tap ̈

Slide 41

Slide 41 text

\X Spınal Tap ̈ n\N{COMBINING DIAERESIS}

Slide 42

Slide 42 text

\X Spınal Tap ̈ n\N{COMBINING DIAERESIS} \r\n

Slide 43

Slide 43 text

\p

Slide 44

Slide 44 text

\p{ASCII}

Slide 45

Slide 45 text

\P{ASCII}

Slide 46

Slide 46 text

\p{General_Category=Letter}

Slide 47

Slide 47 text

\p{Letter}

Slide 48

Slide 48 text

\p{L}

Slide 49

Slide 49 text

\pL

Slide 50

Slide 50 text

L Letter M Mark N Number P Punctuation S Symbol Z Separator C Other

Slide 51

Slide 51 text

S Symbol Sm Math_Symbol Sc Currency_Symbol Sk Modifier_Symbol So Other_Symbol

Slide 52

Slide 52 text

\p{Script=Latin}

Slide 53

Slide 53 text

\p{Latin}

Slide 54

Slide 54 text

[\p{Hiragana} \p{Katakana} \p{Han} \p{Latin} \p{Common}]

Slide 55

Slide 55 text

[\p{Hira} \p{Kana} \p{Hani} \p{Latn} \p{Common}]

Slide 56

Slide 56 text

Arab Arabic Beng Bengali Deva Devanagari Egyp Egyptian hieroglyphs Ethi Ethiopic Grek Greek Hang Hangul …

Slide 57

Slide 57 text

return $word if $word =~ s{ зи $}{г}x || $word =~ s{ е ( \p{Cyrl} ) и $}{я$1}x || $word =~ s{ ци $}{к}x || $word =~ s{ (?: та | ища ) $}{}x;

Slide 58

Slide 58 text

use v5.18;

Slide 59

Slide 59 text

(?[ ])

Slide 60

Slide 60 text

(?[ \d - \p{ASCII} ])

Slide 61

Slide 61 text

(?[ \d & \p{Thai} ])

Slide 62

Slide 62 text

perlre — regex syntax perlrebackslash — regex escape sequences perlrecharclass — regex character classes perlunicode — Unicode features Lingua::Stem::UniNE — code examples

Slide 63

Slide 63 text

@nickpatch