TMTOWTDI

G

Graham Gough

I have been trying to solve a problem in which I wish to substitute
for *almost* all occurrences of a regular expression. For example all
occurrences of \d\d\d\d except some specific ones. I ended up with
using the e modifier and code like that given below, but had a nagging
felling that there was probably some more elegant method. Anyone any
suggestions?

Graham

$text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;

sub choose {
my $val = shift;
my $notpat = shift;
my $ret=$val;
if (!($val =~ /$notpat/)) {
$ret = "XX" . $ret . "XX";
}
return $ret;
}
 
F

fifo

I have been trying to solve a problem in which I wish to substitute
for *almost* all occurrences of a regular expression. For example all
occurrences of \d\d\d\d except some specific ones. I ended up with
using the e modifier and code like that given below, but had a nagging
felling that there was probably some more elegant method. Anyone any
suggestions?

Graham

$text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;

sub choose {
my $val = shift;
my $notpat = shift;
my $ret=$val;
if (!($val =~ /$notpat/)) {
$ret = "XX" . $ret . "XX";
}
return $ret;
}

How about

$text =~ s/(?!1234)(\d{4})/XX$1XX/g;
 
G

Glenn Jackman

fifo said:
I have been trying to solve a problem in which I wish to substitute
for *almost* all occurrences of a regular expression. For example all
occurrences of \d\d\d\d except some specific ones. I ended up with
[...]

How about

$text =~ s/(?!1234)(\d{4})/XX$1XX/g;

Depending on the OP's data, may have undesired matches, for example, if
$text="12345"

Perhaps, if the OP only cares about 4 digit numbers, and suppose he
doesnt want to match 1234 or 6789 or 4242:
$text =~ s/(^|\D)(?!1234|6789|4242)(\d{4})(\D|$)/$1<<$2>>$3/g;
 
A

Anno Siegel

Graham Gough said:
I have been trying to solve a problem in which I wish to substitute
for *almost* all occurrences of a regular expression. For example all
occurrences of \d\d\d\d except some specific ones. I ended up with
using the e modifier and code like that given below, but had a nagging
felling that there was probably some more elegant method. Anyone any
suggestions?

Unless the exceptions can be reasonably described through a regex,
the /e modifier is bound to crop up. Nothing wrong with that.
$text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;

sub choose {
my $val = shift;
my $notpat = shift;
my $ret=$val;
if (!($val =~ /$notpat/)) {
$ret = "XX" . $ret . "XX";
}
return $ret;
}

There are a few keywords that make a Perl programmer think "Hash!", and
one of them is "a few exceptions". (Another such keyword is "unique").
In that vein, I'd go for (leaving out the obvious setup of %exceptions,
and untested)

s/(\d{4})/exists $exceptions{ $1} ? $1 : "XX$1XX"/ge;

Anno
 
U

Uri Guttman

GJ> Depending on the OP's data, may have undesired matches, for example, if
GJ> $text="12345"

GJ> Perhaps, if the OP only cares about 4 digit numbers, and suppose he
GJ> doesnt want to match 1234 or 6789 or 4242:
GJ> $text =~ s/(^|\D)(?!1234|6789|4242)(\d{4})(\D|$)/$1<<$2>>$3/g;

i would still go with /e but just be simpler than the OP's code.

$text =~ s/(\d{4})/ $1 eq '1234' ? $1 : "XX$1XX" /eg;

if you need to exclude many sets of 4 digits, put them in a hash:

my %keepers = map { $_ => 1 } qw( 1234 5678 9012 ) ;

$text =~ s/(\d{4})/ $keepers{$1} ? $1 : "XX$1XX" /eg;

that is clear, fast and modifiable by others.

uri
 
T

Tad McClellan

Anno Siegel said:
There are a few keywords that make a Perl programmer think "Hash!", and
one of them is "a few exceptions". (Another such keyword is "unique").


Yet some more:

lookup
cross-reference
related to ( a "relation" )
associated with
duplicate
symbolic reference
dispatch table


Sometimes you can cut right through that thinking stuff and
head right on over to the solution:

Message-ID: <[email protected]>

I don't know what your original problem is,
but I suggest to use a hash. --Rafael Garcia-Suarez


:)
 
U

Uri Guttman

TM> Yet some more:

TM> lookup
TM> cross-reference
TM> related to ( a "relation" )
TM> associated with
TM> duplicate
TM> symbolic reference
TM> dispatch table

table (of any sort)
record
structure
convert
is a
set
has a
tree
mapping (or map)
dictionary
objects (most use hashes)
attributes
properties
traits
registry

this is actually an interesting list and maybe could be converted into
an FAQ: when should i use a hash? :)

uri
 
G

Graham Gough

Thanks for all the interesting suggestions. Like some of the posters,
my usual reaction to many programming problems is to use a hash, I
don't know why I didn't think of it this time. Old age, probably

Thanks again

Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top