Introducing a character in a string

A

Arun Kumar

Hi,

I want to introduce a special character ( \ ) in a string using perl script.
(i.e) "(e-mail address removed)" has to be converted into "first.last\@mail.com".
Please help me.

Regards,
Arunkumar
 
P

Paul Lalli

Arun Kumar said:
I want to introduce a special character ( \ ) in a string using perl script.
(i.e) "(e-mail address removed)" has to be converted into "first.last\@mail.com".
Please help me.

Have you read any documentation or tutorials about regular expressions?
That would be a very good place to start:

perldoc perlretut

Specifically, you want to use the search-and-replace operators s///.

If you have read it and understand regular expressions, please post a
short but complete piece of code demonstrating what you've tried so far
and how it has failed to function as you'd like it to.

Paul Lalli
 
J

Josef Moellers

Arun said:
Hi,

I want to introduce a special character ( \ ) in a string using perl script.
(i.e) "(e-mail address removed)" has to be converted into "first.last\@mail.com".
Please help me.

The answer depends upon the question:
Why would you need it?

If you need it because perl complains about "Possible unintended
interpolation of @mail in string", then just insert the backslash
character using your favorite editor.

If you _think_ you need the backslash because the @ is a special
character and you _think_ that perl might get confused: it won't. If you
read "(e-mail address removed)" from some place (e.g. STDIN, some file), then
the @ won't be regarded as a special character any more. Introducing a
backslash will only introduce one more character, a character you don't
really want.

If you still are convonced you need the backslash:

$s = '(e-mail address removed)';
$s =~ s/\@/\\$&/;

HTH,

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top