CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Remove everything after a backslash


New User

Nov 6, 2015, 7:29 AM

Post #1 of 2 (1253 views)
Remove everything after a backslash Can't Post

I would like to remove everything that comes after a backslash, but it seems like the backslash is giving me trouble. For example,

I have: servername\hostname
want to end up with: servername

I am using the following command:

$word = "servername\hostname";
$word =~ s/\\.*$//g;
print "$word\n";

but I end up with: servernamehostname.

Its very confusing because when I use the same command but just change everything to forward slashes it works just fine. Something different must be needed. I also tried doing a split, but the same thing occurred. The split just removed the slash and combined server and hostname. I'm sure there is a simple answer, any suggestions anyone could provide would be much appreciated.


Veteran / Moderator

Nov 6, 2015, 8:02 AM

Post #2 of 2 (1247 views)
Re: [binary0101] Remove everything after a backslash [In reply to] Can't Post

There are multiple ways to do this, but the easiest fix to your code would be to use single quotes instead of double quotes around the assignment.

$word = 'servername\hostname';


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives