Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and replace a | for a +

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
here's the string i have:

$sn='Males, 18| Years Old';
$sx =~ s/|/+/g;

Need to have it as:

$sn='Males, 18+ Years Old';

But my search/replace code isn't working.

Any ideas?

- g
 
Code:
$sx =~ s/[red]\[/red]|/+/g;

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
You are pushing two different thing

$sn='Males, 18| Years Old';
$sx =~ s/|/+/g;

$sn is not $sx
also escape those characters

new one

$sn =~ s/\|/\+/ig;
 
Very little needs to be escaped in the right end of a substitution except for the delimiting characters you use (the / in this case), and all the usual things you'd want to escape ($ and @ if you want the literals and not interpolate vars).

Code:
$ perl
my $t = "test|test";
$t =~ s/\|/+/g;
print "$t\n";
__END__
test+test

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Hi

Personally I would say, there is no need for [tt]s///[/tt] substitution, so neither for regular expression, so neither for escaping :
Code:
  [blue]DB<1>[/blue] $sn='Males, 18| Years Old';

  [blue]DB<2>[/blue] $sn=~[url=http://perldoc.perl.org/functions/y.html]y[/url]/|/+/;

  [blue]DB<3>[/blue] print $sn;
Males, 18+ Years Old

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top