Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:
I have a file containing 'regex' where number ranges are specified like this [21-43] (match any number from 21..43) I have a bit of code to translate this to Perl regex. It works but the generated regex is not so efficient as it could be. What would be much nicer is if for the example above I could get it to generate /(2[123456789])|(3\d)|(4[0123])/. Can anyone think of non-clunky way to do this ? BTW these numbers are IP address ranges with all that implies.
# Expand ranges of numbers so [21-25] becomes (21|22|23|24|25) # Ugh! # if ( $ip =~ /-/ ) { # foreach (split(/\./, $ip) { my @i=(split(/\./, $ip) foreach (@i) { if ( /\[(\d+)-(\d+)\]/ ) { $_ = "(" . join("|", ($1..$2)) . ")"
Janitored by Arunbear - balanced code tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: converting user friendly regex to perl friendly
by ikegami (Patriarch) on Oct 15, 2004 at 18:17 UTC | |
by ikegami (Patriarch) on Oct 15, 2004 at 18:25 UTC | |
|
Re: converting user friendly regex to perl friendly
by abitkin (Monk) on Oct 15, 2004 at 17:57 UTC | |
|
Re: converting user friendly regex to perl friendly
by Random_Walk (Prior) on Oct 15, 2004 at 19:29 UTC | |
by Anonymous Monk on Oct 15, 2004 at 20:04 UTC |