Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

How to remove underscore before every dot using perl?

by finddata (Sexton)
on Mar 16, 2017 at 06:51 UTC ( #1184834=perlquestion: print w/replies, xml ) Need Help??

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

Input: DEMO_.perl DEMO_.php
$str =~ tr/a-zA-Z//dc
Expected: DEMO.perl DEMO.php

Replies are listed 'Best First'.
Re: How to remove underscore before every dot using perl?
by Athanasius (Bishop) on Mar 16, 2017 at 07:05 UTC

    Hello finddata,

    For the transliteration operator, the /c modifier complements the searchlist, and the /d modifier causes found-but-not-replaced characters to be deleted. So the expression tr/a-zA-Z//dc says: Delete any character that is not a letter. But that includes the dot. So you need to add in a dot, to say: Delete all characters except letters and dots:

    $str =~ tr/a-zA-Z.//dc;

    But in this case I think it would be clearer to use an explicit substitution:

    $str =~ s/_\././;

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How to remove underscore before every dot using perl?
by madtoperl (Friar) on Mar 16, 2017 at 09:50 UTC
    Hi finddata,
    You can try this for your current input data assuming only one _ is there,
    #!/usr/local/bin/perl use strict; use warnings; my $str = 'DEMO_.perl'; $str =~ s/\_//; print $str;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2020-09-29 12:59 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (146 votes). Check out past polls.