http://www.perlmonks.org?node_id=1184834

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;