By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

search . and replace with /../

P: 7
I have writen a small script to replace the . with /../
which is as follows.

$var = "DataAccess\DataAccess.";
$var =~ s/(.)/(\)..(\)/;
print "value is : $var";

But in this case when I run this script it gives me result as :
value is : ()..()ataAccessDataAccess.

which not as per my expectation.

I want the result some thing like this : value is : value is : DataAccess\DataAccess\..\

So what changes should i make in my current script to get desire result.
Also why it is removing '\' character when run the script.

-Thanks,
Raju
Jul 8 '08 #1
Share this Question
Share on Google+
6 Replies


gpraghuram
Expert 100+
P: 1,275
. is a special character and when u want to replace that you shuld use escape character for that like
Expand|Select|Wrap|Line Numbers
  1. $val =~ s/\./\.\./g;
  2.  
  3.  
Raghu
Jul 8 '08 #2

P: 7
. is a special character and when u want to replace that you shuld use escape character for that like
Expand|Select|Wrap|Line Numbers
  1. $val =~ s/\./\.\./g;
  2.  
  3.  
Raghu
Thanks Raghu for your usefull solution...
But One problem is still there..when i run the following script

$var = "DataAccess\DataAccess.";
$var =~ s/\./\\.\./;

then output is : DataAccessDataAccess\..

but in this output why '\' is removed ???....I want this in
output : DataAccess\DataAccess\.. formate

Can you please suggest me some solution to maintain this \ in my output string.

Thanks,
Raju
Jul 8 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
you changed the regexp that Raghu posted for you, it should be:

Expand|Select|Wrap|Line Numbers
  1. s/\./\.\./g;
should not be:

Expand|Select|Wrap|Line Numbers
  1. s/\./\\.\./g;
Next time look at the code more carefully.

It can really be written like this:

Expand|Select|Wrap|Line Numbers
  1. $foo = 'this\that.';
  2. $foo =~ s/\./../g;
  3. print $foo;
the '.' has no special meaning on the replacement side of the regxp, only on the search side.
Jul 8 '08 #4

gpraghuram
Expert 100+
P: 1,275
the '.' has no special meaning on the replacement side of the regxpThis is a good learning for me.....

Raghuram
Jul 9 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
the '.' has no special meaning on the replacement side of the regxpThis is a good learning for me.....

Raghuram

The replacement side of s/// is treated like a double-quoted string, so variable expansion (for example $foo and @foo) and meta character expansion (for example \t and \n) do occur, but a dot in a double-quoted string is just a dot. So anything that can be interpolated/expanded in a double-quoted string will also be expanded/interplolated on the replacement side of s///. Everything else is treated literally. There is a way to alter that behavior however, namely using the "e" modifier on the end of the regexp.
Jul 9 '08 #6

P: 7
Thanks to all !!!!
Finaly solution is working for me....Great Forum !!!!!
Jul 14 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.