Eugene Sapozhnikov
Eugene Sapozhnikov

Reputation: 23

How to add a character before a specific symbol in a variable?

I have a user defined variables that is a [email protected]

Using perl I need to add "sc" at the end of user and before the @ symbol.

so if:

$user = "[email protected]"
$string = "sc"

I need to come out with the result of

$user-id = "[email protected]"

Any help would be appreciated.

Upvotes: 2

Views: 416

Answers (1)

ikegami
ikegami

Reputation: 386501

$user =~ s/(?=@)/sc/;

or

$user =~ s/@/sc@/;

or

$user =~ s/^[^@]*\K/sc/;    # Assumes "@" will always be present.

Of course, none of those will work if $user doesn't contain the correct string to begin with.

$user = "[email protected]";

is the same as

$user = "user" . join($", @domain) . ".com";

Given that @domain doesn't exist, that's the same as

$user = "user.com";

Always use use strict; use warnings;! You want

my $user = "user\@domain.com";

or

my $user = '[email protected]';

to create var $user with the string [email protected].

Upvotes: 5

Related Questions