XML technologies / Regular expressions / Practical regex techniques / A few short examples / Expected data and assumptions

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

This is a good point to discuss general issues in building regexes, some already mentioned briefly. Using regexes on data in particular situations usually involves implicit assumptions, which often deserve conscious attention. Even something as simple as ˹a˼ makes assumptions about the character set (see Characters). Common sense usually suffices, which is why I have said little about this.

Yet an assumption obvious to one person may not be obvious to another. The previous section’s solution implicitly assumes that escaped newlines should not match, except in dot-matches-all mode. If the dialect supports it, the regex can simply have its dot replaced with ˹(?s:.)˼.

The data may also differ greatly from expectations. Apply our regex to source code in almost any language and it will fail unexpectedly: comments may contain quotation marks that need not be paired.

Making assumptions about expected data or regex usage is not wrong. Problems arise when assumptions are too optimistic or when a regex is used in situations its author did not anticipate.

  

 

 

 

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