stripping characters from a string
Anonymous Monk
created: 2006-03-06 01:47:23
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!
Re: stripping characters from a string
created: 2006-03-06 02:02:04
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
created: 2006-03-06 02:07:09
You might try reading the [perldoc://perlre] and [perldoc://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
created: 2006-03-06 02:21:02

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';

Re^2: stripping characters from a string
created: 2006-03-06 04:41:32

Or rather: tr/ '/_ /;

I'd prefer this, actually:

for ($string) { tr/ '/_ /; print }
Re^3: stripping characters from a string
created: 2006-03-06 04:59:26

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///.

Regards,
Velusamy R.


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

Re^4: stripping characters from a string
created: 2006-03-06 05:23:45
ah, "and remove any '". I know what y/// is, I just don't know why anyone would use it. Why do you have a \ ?
Re: stripping characters from a string
created: 2006-03-06 09:30:12

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..)

Re^2: stripping characters from a string
created: 2006-03-06 14:35:01

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 become 1 '_'
$fus =~ s/^_|_$//g;            #at most one on each end, because of the above
<-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 [http://en.wikipedia.org/wiki/Trebuchet|trebuchet]

perlmonks.org content © perlmonks.org and acid06, Anonymous Monk, ayrnieu, lamp, leocharre, radiantmatrix, Samy_rio

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03