XML technologies / Regular expressions / Practical regex techniques / A few short examples / Avoiding unwanted matches
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
It is easy to overlook what happens when input differs from expectations. Suppose a filter converts plain text to HTML, replacing runs of hyphens with an <HR> tag, a horizontal bar across the page: HR means horizontal rule. The substitution s/-*/<HR>/ does replace hyphen runs with bars, but only at line beginnings. Surprised? Well, s/-*/<HR>/ prefixes every line with an <HR>, whether or not hyphens exist.
Again, every optional element of a regex always matches. Applied to a string, ˹-*˼ matches one or more initial hyphens. But with no hyphen, it matches too: the empty text at the string’s start. That is what star means.
I found a similar example in a well-known author’s book. It develops a regex for numbers, including floating-point values: optional minus, any number of digits, optional decimal point, and any number of fractional digits. The regex is ˹-?[0-9]*\.?[0-9]*˼.
This does match text such as 1, -272.37, 129238843., .191919 and even -.0, as expected.
But does it match ›Dieser●Test●enthält●keine●Ziffern‹, ›Nix●da‹ or even the empty string? Look closely: everything is optional. If a digit occurs and if it occurs at the start, it matches, but none of that is required! The regex matches all three nonnumeric texts by matching the empty text at their beginnings. It even matches the empty text at the start of ›Nummer●123‹ because that empty match starts before 123.
Say what you mean. A floating-point number must contain at least one digit, or it is not a number. A better regex first requires at least one digit before the decimal point, using plus: ˹-?[0-9]+˼. In the optional fractional part, digits must be allowed only if a point is present too. Naively using ˹\.?[0-9]*˼ would let ˹[0-9]*˼ match without a preceding point.
Instead, state exactly what is wanted: a decimal point and any following digits are optional together: ˹(\.[0-9]*)?˼. The question mark now quantifies the whole combination of decimal point and fractional digits, not just the point. Inside that combination, the point is mandatory. Without it, the engine can never reach ˹[0-9]*˼.
Putting it together gives ˹-?[0-9]+(\.[0-9]*)?˼. It does not match things like ›.007‹ because at least one digit is required before the point. Allowing zero digits there would require at least one after it: the entire expression must contain a digit, or we return to the original problem.
Add an alternative covering the previously forbidden case: ˹-?[0-9]+(\.[0-9]*)?|-?\.[0-9]+˼. This also allows a point followed by one or more digits, with an optional minus beforehand—easy to forget. Details, details. We can group the alternation and put ˹-?˼ only once before it: ˹-?([0-9]+(\.[0-9]*)?|\.[0-9]+)˼.
This improves the original, but still matches ›2003.04.12‹. Often the expected data or surrounding regex excludes certain characters before and after the number. Our floating-point regex works only within a larger expression, for example ˹^...$˼ or ˹Wert\s*=\s*...$˼.
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