Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

stripping characters from a string

by Anonymous Monk
on Mar 06, 2006 at 06:47 UTC ( [id://534640]=perlquestion: print w/replies, xml ) Need Help??

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

hi, i just started learning perl, so sorry if this question may sound very newbish.

I have a string that i want to replace spaces with an under score and remove any '.

i know i need some sort of regex but i really don't understand how to start.

$string = "Good O'l Time";

will look like this with the regex:

Good_Ol_Time

I would appreciate any help or referrence :)

thanks!

Replies are listed 'Best First'.
Re: stripping characters from a string
by acid06 (Friar) on Mar 06, 2006 at 07:07 UTC
    You might try reading the perlre and perlretut manpages.
    You can follow the links above for an online reading or you can try it on your own shell/command prompt using:
    perldoc perlre perldoc perlretut


    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="
Re: stripping characters from a string
by Samy_rio (Vicar) on Mar 06, 2006 at 07:21 UTC

    Try this,

    use strict; use warnings; my $string = "Good O'l Time"; $string =~ y/ '/\_/d; print $string;

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      I'd prefer this, actually:

      for ($string) { tr/ '/_/d; print }

        ayrnieu, OP's need

        Good_Ol_Time

        but your output shows

        Good_O l_Time

        As per perlfunc - Perl builtin functions tutorial.

        y///
        The transliteration operator. Same as tr///.

        Updated: ayrnieu updated his answer without proper information.

        Regards,
        Velusamy R.


        eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: stripping characters from a string
by lamp (Chaplain) on Mar 06, 2006 at 07:02 UTC
    Hi,
    Please try the following code.
    $string = "Good O'l Time"; $string =~ s/\ /_/g; $string =~ s/\'//g; print $string;

    Thanks,
    --lamp
Re: stripping characters from a string
by leocharre (Priest) on Mar 06, 2006 at 14:30 UTC

    if you're trying to normalize a string so you can make a decent filename out of it- which i get the feeling here (slap me if i'm wrong..)

    my $fus=qq|This Is some Funkah Lookin' & Zmellin' "straeyang"|; $fus=~s/\W/_/g; # now $fus equals This_Is_some_Funkah_Lookin____Zmellin_ __straeyang_ #great. super. let's clean that up $fus=~s/_+/_/g; # now $fup equals This_Is_some_Funkah_Lookin_Zmellin_straeyang_ #let's get rid of any _ in ends $fus=~s/^_+|_+$//g; print $fus; # prints This_Is_some_Funkah_Lookin_Zmellin_straeyang #maybe im tight and i don't want any uppercase $fus = lc($fus); #now $fus = this_is_some_funkah_lookin_zmellin_straeyang #anyway.. you get the idea

    Maybe you want to get rid of funny chars? like non word chars? then use \W, that means *any* non word characters, as opposed to (\w which are 0-9, a-z, A-Z, _ anyway..)

      Good post. Here's what I would do if all I wanted was your last result:

      # note the lower-casing inline ($fus = lc $fus) =~ s/\W+/_/g; #all spans of 1 or more non-words becom +e 1 '_' $fus =~ s/^_|_$//g; #at most one on each end, because of th +e above

      But, for the poster's question, removing all single-quotes and migrating spaces to _...

      for ($string) { s/\s+/_/g; #convert 1+ spaces to a single '_' s/'|^_|_$//g; #trim lead/trail _ and remove single quotes }

      Which is going a bit further than the direct solution of

      $string =~ tr/\s'/_/d;
      <-radiant.matrix->
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found