how to convert a string to an escaped string

Discussion in 'Perl Misc' started by Gregory Toomey, Feb 10, 2004.

  1. Gunnar Hjalmarsson wrote:

    > Lowell Kirsh wrote:
    >> Very funny...

    >
    > Glad you think so. Don't top post, btw.
    >
    >> How about a perl function that can convert a string to
    >> another string in which special chars are represented as escaped
    >> versions of themselves...

    >
    > Yes, there is such a function.
    >
    > perldoc -f quotemeta
    >
    > Why didn't you just look it up instead of posting here?


    Thats a stupid statement.

    This group reeks of arrogance. People ask legitmate questions and are
    "baited" by the regulars.

    gtoomey
     
    Gregory Toomey, Feb 10, 2004
    #1
    1. Advertising

  2. Gregory Toomey

    Lowell Kirsh Guest

    I want to convert:
    blah blah "foo" blah
    to
    blah\ blah\ \"foo\"\ blah

    Is there an easy way to do it?

    Lowell
     
    Lowell Kirsh, Feb 11, 2004
    #2
    1. Advertising

  3. Lowell Kirsh wrote:
    > I want to convert:
    > blah blah "foo" blah
    > to
    > blah\ blah\ \"foo\"\ blah


    Seems as if you just did so.

    > Is there an easy way to do it?


    Wasn't the way you used easy enough?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #3
  4. Gregory Toomey

    Lowell Kirsh Guest

    Very funny... How about a perl function that can convert a string to
    another string in which special chars are represented as escaped
    versions of themselves...

    Gunnar Hjalmarsson wrote:

    > Lowell Kirsh wrote:
    >
    >> I want to convert:
    >> blah blah "foo" blah
    >> to
    >> blah\ blah\ \"foo\"\ blah

    >
    >
    > Seems as if you just did so.
    >
    >> Is there an easy way to do it?

    >
    >
    > Wasn't the way you used easy enough?
    >
     
    Lowell Kirsh, Feb 11, 2004
    #4
  5. Lowell Kirsh wrote:
    > Very funny...


    Glad you think so. Don't top post, btw.

    > How about a perl function that can convert a string to
    > another string in which special chars are represented as escaped
    > versions of themselves...


    Yes, there is such a function.

    perldoc -f quotemeta

    Why didn't you just look it up instead of posting here?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #5
  6. Gregory Toomey wrote:
    > Gunnar Hjalmarsson wrote:
    >> Lowell Kirsh wrote:
    >>> How about a perl function that can convert a string to another
    >>> string in which special chars are represented as escaped
    >>> versions of themselves...

    >>
    >> Yes, there is such a function.
    >>
    >> perldoc -f quotemeta
    >>
    >> Why didn't you just look it up instead of posting here?

    >
    > Thats a stupid statement.
    >
    > This group reeks of arrogance. People ask legitmate questions and
    > are "baited" by the regulars.


    First: My limited Perl knowledge doesn't qualify me to be called a
    "regular".

    Since OP wondered if there is a function in Perl that escapes special
    characters, the natural first step should reasonably have been to look
    in perldoc perlfunc. If he had done so, he would most likely have
    found it.

    Asking hundreds of people for help with looking up things in the
    documentation is not a legitimate step, at least not when the answer
    can be as easily found as in this case. Pointing out that is neither
    stupid nor arrogant.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #6
  7. Lowell Kirsh <> wrote in message news:<c0ct1k$qgu$>...
    > I want to convert:
    > blah blah "foo" blah
    > to
    > blah\ blah\ \"foo\"\ blah
    >
    > Is there an easy way to do it?
    >
    > Lowell


    #!/bin/perl
    $A = 'dogs "bark" and cats "meow"';
    print "$A\n";
    $A=~s- -\\ -g;
    print "$A\n";

    give
    !./a.pl
    dogs "bark" and cats "meow"
    dogs\ "bark"\ and\ cats\ "meow"
    [Hit return to continue]

    $A=~ s- -\\ -g

    =~ says evaluate this expression
    s says substitute
    - next charater after s is a delimiter
    the three delimiters define a before and after
    the before is a " "
    the after is a "\"
    g says global. do it for all occurrentces of the before pattern
    (instead of the first.
     
    Jon Landenburer, Feb 11, 2004
    #7
  8. Gregory Toomey

    Bart Lateur Guest

    Lowell Kirsh wrote:

    >I want to convert:
    > blah blah "foo" blah
    >to
    > blah\ blah\ \"foo\"\ blah
    >
    >Is there an easy way to do it?


    quotemeta()?

    Otherwise, try

    s/(?=[CHARLIST])/\\/g;

    with CHARLIST replaced by a list of characters/ range of characters, for
    example

    s/(?=[\\\s\"])/\\/g;

    to escape whitespace, quotes, and backslashes.

    --
    Bart.
     
    Bart Lateur, Feb 12, 2004
    #8
    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. Replies:
    2
    Views:
    768
  2. Petr Jakes
    Replies:
    3
    Views:
    360
    Petr Jakes
    Nov 12, 2005
  3. ldng
    Replies:
    3
    Views:
    1,817
    Tim Golden
    May 10, 2007
  4. Guest
    Replies:
    2
    Views:
    760
    Guest
    Jun 20, 2007
  5. Just Another Victim of the Ambient Morality

    How do I convert escaped HTML into a string?

    Just Another Victim of the Ambient Morality, Nov 24, 2007, in forum: Python
    Replies:
    6
    Views:
    618
    Bruno Desthuilliers
    Nov 24, 2007
Loading...

Share This Page