palikexxl
palikexxl

Reputation: 41

how to replace specific character(s) in string by number(s)

I would like to replace a string specific character(s) with numbers.

lets assume I have such format string "B######" so it has one "letter" and 6 "#" characters. My need is to first figure out how many "#" it contains and based on this number, will generate random token

Session::Token->new(alphabet => ['0'..'9'], length => $length_from_format_string);

then, I need to replace that #... with the generated number. BUT...

format string could be also B##CDE###1 so it still has 6 "#" so generated number must be divided according to format :( and all this should be as effective as possible

Thanks for your hints

Upvotes: 0

Views: 80

Answers (1)

Sobrique
Sobrique

Reputation: 53478

Regular expressions (in perl) can have functions embedded if you use the e flag. Adding the g modifier will do it multiple times.

So:

my $string = "B##CDE###1";
$string =~ s/\#/int rand(10)/ge;
print $string;

Upvotes: 5

Related Questions