Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to remove underscore at the end of the line?

by finddata (Sexton)
on Mar 17, 2017 at 04:25 UTC ( #1184955=perlquestion: print w/replies, xml ) Need Help??

finddata has asked for the wisdom of the Perl Monks concerning the following question:

i like to remove the underscore at the end for the following line.
DCMS_DEMO_ DCMS_DEMO_new_block2_ DCMS_DEMO_new_block2_checklist_tmp_ DCMS_DEMO_new_block2_checklist_tmp_rev1_
code which i tried:
$subject =~ s/_(?=[^_]*$)//;
obtained output:
DCMS_DEMO DCMS_DEMOnew_block2 DCMS_DEMOnew_block2checklist_tmp DCMS_DEMOnew_block2checklist_tmprev1
Expected output:
DCMS_DEMO DCMS_DEMO_new_block2 DCMS_DEMO_new_block2_checklist_tmp DCMS_DEMO_new_block2_checklist_tmp_rev1

Replies are listed 'Best First'.
Re: How to remove underscore at the end of the line?
by afoken (Canon) on Mar 17, 2017 at 07:47 UTC
Re: How to remove underscore at the end of the line?
by Anonymous Monk on Mar 17, 2017 at 04:39 UTC
    s/_$//
      Also, the "code you tried" does NOT produce the "output you obtained," so I'm not sure what you're actually doing.
      But it is not displaying the expected output as per you mentioned.

        Then you must be using it wrong. Show us how you used the solution posted.

        I am confident that s/_$// works for the sample data you posted. Show us your code.


        Dave

        Just to drive davido's point home:

        c:\@Work\Perl\monks>perl -wMstrict -le "for my $s (qw( DCMS_DEMO_ DCMS_DEMO_new_block2_ DCMS_DEMO_new_block2_checklist_tmp_ DCMS_DEMO_new_block2_checklist_tmp_rev1_ )) { my $subject = $s; print qq{'$subject'}; $subject =~ s/_(?=[^_]*$)//; print qq{'$subject' \n}; } " 'DCMS_DEMO_' 'DCMS_DEMO' 'DCMS_DEMO_new_block2_' 'DCMS_DEMO_new_block2' 'DCMS_DEMO_new_block2_checklist_tmp_' 'DCMS_DEMO_new_block2_checklist_tmp' 'DCMS_DEMO_new_block2_checklist_tmp_rev1_' 'DCMS_DEMO_new_block2_checklist_tmp_rev1'
        That's with your original regex, and I get the same output with  s/_$// also.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1184955]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2020-01-17 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?