kepler has asked for the wisdom of the Perl Monks concerning the following question:
Good evening
I don't know this one, and don't know if it's possible. I have a long string, let's say $str; I want to substitute all the pattern occurences (supose /\d+/) by one function with the match as argument. So assume, after each match, that the function is: sub add(){($v) = @_;return ($v*4);}How can I do this?$str =~ s/\d+/&add($1)/gi; doesn't work...and seems terribly wrong...I've placed a simple function, but it can be much more complex.
Regards,Kepler
EDIT: $str =~ s/\d+/&add($1)/ge; works... :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: String replace with function
by GrandFather (Saint) on Sep 19, 2016 at 21:12 UTC | |
by shmem (Chancellor) on Sep 19, 2016 at 21:33 UTC | |
by AnomalousMonk (Archbishop) on Sep 19, 2016 at 23:08 UTC | |
|
Re: String replace with function
by shmem (Chancellor) on Sep 19, 2016 at 21:16 UTC | |
|
Re: String replace with function
by Anonymous Monk on Sep 19, 2016 at 21:05 UTC |