Can Regular Expressions do Integer Ranges?

Advertisement
Is there an easy way of testing an expression for a range of numbers e.g. a range of documents from
?DOC:173? to ?DOC:537?
thanks,
Phil
Advertisement

Replay

Regular expressions are not the cure for cancer. So you could have such problems.
It's easier to check if the expression matches "\?DOC:(\d\d\d)\?" - a three-digit number from 000 to 999, and then get the first group, evaluate the numeric value, and make the comparison using the numeric value.
Perl regex experts can tell you that you can embed expressions (like arithmetic and so on) in Perl regular expressions, but it is not implemented in Java. (Imagine if such things were ready for use in Java...)
An excerpt from the "perlretut - Perl regular expressions tutorial " that you can find in your Perl distro: (mine is ActiveState's ActivePerl)
A bit of magic: executing Perl code in a regular expression
Normally, regexps are a part of Perl expressions. Code evaluation expressions turn that around by allowing arbitrary Perl code to be a part of a regexp. A code evaluation expression is denoted (?), with code a string of Perl statements.
Code expressions are zero-width assertions, and the value they return depends on their environment. There are two possibilities: either the code expression is used as a conditional in a conditional expression (?(condition)...), or it is not. If the code expression is a conditional, the code is evaluated and the result (i.e., the result of the last statement) is used to determine truth or falsehood. If the code expression is not used as a conditional, the assertion always evaluates true and the result is put into the special variable $^R. The variable $^R can then be used in code expressions later in the regexp. Here are some silly examples: [/i]