in reply to Re: Regular expression patter matching question
in thread Regular expression pattern matching question
You can simply #2 a a bit:
That rx can be built dynamically as follows:$rx_4333 = /(?=\d{4}$) (?: 4 (?: 3 (?: 3 [3-9] | [4-9] ) | [4-9] ) | [5-9] ) $/x
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 |