Introducing a character in a string

Discussion in 'Perl Misc' started by Arun Kumar, Jan 17, 2005.

  1. Arun Kumar

    Arun Kumar Guest

    Hi,

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

    Regards,
    Arunkumar
    Arun Kumar, Jan 17, 2005
    #1
    1. Advertising

  2. Arun Kumar

    Paul Lalli Guest

    "Arun Kumar" <> wrote in message
    news:...
    > I want to introduce a special character ( \ ) in a string using perl

    script.
    > (i.e) "" 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
    Paul Lalli, Jan 17, 2005
    #2
    1. Advertising

  3. Arun Kumar wrote:
    > Hi,
    >
    > I want to introduce a special character ( \ ) in a string using perl script.
    > (i.e) "" 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 "" 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 = '';
    $s =~ s/\@/\\$&/;

    HTH,

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Jan 17, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,770
    Joerg Jooss
    Jan 19, 2006
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    709
    those who know me have no need of my name
    May 28, 2006
  3. herman
    Replies:
    5
    Views:
    7,573
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  4. PerlFAQ Server
    Replies:
    0
    Views:
    378
    PerlFAQ Server
    Jan 25, 2011
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    701
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page