XML technologies / Regular expressions / Features and dialects / Strings, character encodings and modes / Character encodings

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

A character encoding (character encoding) is merely an agreement on interpreting byte values. Decimal byte 110 means ‘n’ in ASCII, but ‘>’ in EBCDIC. Why? An organization decided so at some point. Neither encoding is inherently better; the byte is identical, only its interpretation differs.

ASCII defines only half the possible byte values. The ISO-8859-1 character set, also called Latin-1, fills the other half with accented and special characters suitable for Central Europe. Decimal character 234 means ê in this encoding but is undefined in ASCII.

Our question is how to make a Perl program interpret bytes in a particular encoding. Four decimal bytes 234, 116, 101 and 115 interpreted as Latin-1 form the French word “êtes”. We want regexes ˹^\w+$˼ and \b to match. This requires that metacharacters \w and \b understand Latin-1 characters.

Encoding support

Many encodings exist. Before using one, check the program’s support:

  • Does the program understand the encoding?

  • How do I specify the encoding to use?

  • How complete is its regex support?

Regex support has several aspects:

  • Are multibyte encodings supported? Do dot or [^x] match individual characters or bytes?

  • Do metacharacters \w, \d, \s, \b and so on understand the encoding’s characters? If ê is alphabetic, do \w and \b recognize it too?

  • Are character-class ranges extended? Is ê matched by ˹[a-z]˼?

  • Does case-insensitive matching work for all characters? For example, are ê and Ê treated alike?

Some questions are trickier than they seem. Sun’s java.util.regex package understands all alphabetic Unicode characters occurring in words, yet \w matches only 7-bit ASCII characters. More examples follow later in this chapter.

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top