in reply to Re: Regular expression patter matching question
in thread Regular expression pattern matching question

You can simply #2 a a bit:

$rx_4333 = /(?=\d{4}$) (?: 4 (?: 3 (?: 3 [3-9] | [4-9] ) | [4-9] ) | [5-9] ) $/x
That rx can be built dynamically as follows:
my @digits = $ProdBuild =~ /([0-9])([0-9])([0-9])([0-9])$/; my $rx = '(?=\\d{4}$)'; for (@digits) { $rx .= "(?:$_"; } for (reverse @digits) { local $_ = $_+1; $rx .= '|' . ($_ == 9 ? 9 : "[$_-9]") if $_ != 10; $rx .= ')'; } # 4333 gives (?=\d{4}$)(?:4(?:3(?:3(?:3|[4-9])|[4-9])|[4-9])|[5-9]) foreach (@pattern) { print("Match: $_\n") if /$rx/; }

Update: I cleaned up the regexp building code a bit, at the cost of a little redundancy in the regexp. For example, a last digit of 3 results in 3|[4-9].

Replies are listed 'Best First'.
Re^3: Regular expression patter matching question
by diotalevi (Canon) on Jan 19, 2006 at 20:07 UTC

    Ah. Just so. I like the regex and how you eliminated my wildcards.

    The generation code was kind of ugly.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊