Dan S.
Dan S.

Reputation: 162

perl How to replace one occurence of a character with two

I would like to translate all instances of a character with two characters. The usual way I would do it is:

$text =~ s/a/aa/g;

I only want single instances of a character to be doubled. So aa would remain aa and not turn into aaaa.

I am thinking I have to use variables in the s/// statement but I cannot find any suitable pattern here or on the net.

Upvotes: 2

Views: 635

Answers (1)

amon
amon

Reputation: 57620

Match instances of a that are not next to another a:

s/(?<!a)a(?!a)/aa/g;

Upvotes: 5

Related Questions