Re: substitution
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Tue, 01 Jul 2008 18:46:51 +0100
nico io wrote:
I have wrote this :
s/(?<![0-9A-Za-z;])(\d+)(?!(\;\d+|\d+|\;\s+\d+\;\d+))/$hashTable{$1}/g
it works but when a number is not as a key of the hash table it replace
the numer by nothing.
I would like to let the number if it doesn't belong to my hash table,
how can I do ?
Please keep your posts confined to the perl.beginners group so that others can
both help and be helped by your questions.
Please bottom-post your responses to the perl.beginners group so that extended
threads remain comprehensible. Thank you.
You should always put
use strict;
use warnings;
at the start of your program, which would have given you a warning when missing
keys were substituted.
Your regex isn't clearly thought-out. For instance
/(\d+)(?!(\;\d+|\d+|\;\s+\d+\;\d+)/ says that the string can't be followed by a
semicolon and digits, or just digits, or a semicolon and whitespace and digits.
Because (/d+) is greedy and will consume all the digits available there is no
need to specify anything other than /(\d+)(?!;)/.
You can stop missing keys being substituted by making your replacement string an
expression. This should do what you want:
$str =~ s/(?<![A-Z;])(\d+)(?!;)/ exists $hash{$1} ? $hash{$1} : $1 /gei;
HTH,
Rob
.
- Follow-Ups:
- Re: substitution
- From: Epanda
- Re: substitution
- References:
- substitution
- From: Epanda
- Re: substitution
- From: Rob Dixon
- substitution
- Prev by Date: AW: AW: @INC and cross-platform path usage
- Next by Date: kill process when file number reached...
- Previous by thread: Re: substitution
- Next by thread: Re: substitution
- Index(es):
Relevant Pages
|