#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    129
    Rep Power
    0

    Remove backslash from string


    How can I remove a backslash from a string? I'm having some problems as its the escape character.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    55
    Hi!

    Just escape the backslash, with ... a backslash
    Perl Code:
    $string = 'lala\blabla';
    $string =~ s/\\//;
    print "$string\n";

    Regards, mawe
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,878
    Rep Power
    3890
    Purely as a matter of interest, why do you need to remove a backslash from a string?
  6. #4
  7. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,528
    Rep Power
    1055
    As per mawe's method, i assume you'd want to replace ALL backslashes....

    $subject =~ s/\\//g;

    Like ishnid though, i'm interested in why you would need to do that...
    Last edited by LinuxPenguin; January 31st, 2006 at 04:49 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0
    What if you have a backslash at the end of the string
    $string = 'lalablabla\';

    How would you remove it?

    Thanks,
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    846
    Rep Power
    500
    If you want to remove the backlash only at the end of your string:

    Code:
    $string =~ s/\\$//;
    #
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0
    It's not working.

    $string = 'lalablabla\';
    $string =~ s/\\$//;
    print "$string\n";
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0
    It's not working.

    $string = 'lalablabla\';
    $string =~ s/\\$//;
    print "$string\n";
  16. #9
  17. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,274
    Rep Power
    0
    The substitution works just fine. If you got an error, it was because you didn't build a valid string.

    Can't find string terminator "'" anywhere before EOF
    But this works just fine:
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $string = 'lalablabla\\';
    $string =~ s/\\$//;
    print "$string\n";

    Comments on this post

    • Laurent_R agrees
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0
    jvbnvn

IMN logo majestic logo threadwatch logo seochat tools logo