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!

substituting 2

Status
Not open for further replies.

doood

Technical User
Jun 18, 2001
13
GB
Please can anyone help...
I am trying to substitute '(*)' and replace with '<z9b15>(*)<z$b$>'
I am not having much luck!
Many thanks
 
You'll have to do a little better on your explanation - you want to find

left parenthesis followed by an asterisk followed by right parenthesis

and replace that with

<z9b15>(*)<z$b$>

do I have that right?
Hardy Merrill
Mission Critical Linux, Inc.
 
Thank you for your response -

That is exactly what I need to achieve - they are control codes for QuarkXpress that I need to wrap around the (*) to adjust its size - just in case you were interested!

Hope you can be of help...

Cheers

Duncan
 
What you need to do is use a regular expression and escape the special characters (the parens and the asterisk). The easiest way to do that is to use the special escape metacharacters \Q and \E:
Code:
$string =~ s/\Q(*)\E/\Q<z9b15>(*)<z$b$>\E/g;
I think that will do what you need.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Dear Tracy

Many thanks for your reply - I am very grateful for your help

Unfortunately I am getting the following result using your regexp

\\(\*\)
Does this make any sense?

Kind Regards

Duncan
 
I suspect you have $string = instead of $string =~ and possibly no s in front of the first /.
Then it would just be assigning the escaped string to $string instead of doing a substitution. In the example I gave above, it is assumed that the data you want to change is stored in $string. Substitute your own variable for $string in the expression. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi Tracy

Thanks again for your reply

This is the relevant code copied & pasted from my program...

open(INFILE, 'file.txt')
while (<INFILE>) {
$cell =~ s/\Q(*)\E/\Q<z9b15>(*)<z$b$>\E/g;
}

I have placed the \Q...\E codes around the substitute FROM/TO values as you suggested but I am afraid no joy - I do have the tilde as required - I have tried to be very careful to get the syntax correct

I can get the substitute to work if I don't use metacharacters - i.e. While testing I can search for (*) and replace with -----(*)----- This is not proving to be a problem

Any more ideas?

Sorry to be a bore!

Duncan

P.S. I am a VERY new to Perl
 
It isn't pretty but it seems to work:

my ($string);
open (FH, &quot;Testfile&quot;) || die &quot;could not open file: $!&quot;;
while (defined($string=<FH>))
{
$string =~ s/\(\*\)/\<z9b15\>\(\*\)\<z\$b\$\>/;
}


wvdw
 
It works!!!

I have no idea how though!

Could you please explain briefly how the program works as I do not know how to replace your example for mine in my program.

Why does the substitution work in your program but not in my example? Even if I copy your substitute line out of your program into mine it still does not work!!!

You certainly know your stuff!

Many thanks

Look forward to hearing from you...

Duncan
 
To problem with the code you posted is that $cell does not contain anything. The while statement assigns the value of each record to $_ unless you specify otherwise. That's why wvdw's code works: he assigns each record to a string and then substitutes on that string. Change your code like this:
Code:
open(INFILE, 'file.txt')
while ($cell = <INFILE>) {
$cell =~ s/\Q(*)\E/\Q<z9b15>(*)<z$b$>\E/g;
}
You really don't need the &quot;defined&quot; part, as an empty string will also cause the while to end.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Whats with the \Q and \E?
to escape a charactor you just put \ in front of it
e.g.
$mevar =~ s/\(\*\)/<z9b15>\(\*\)<z|$b\$>/gi;
? that would search and replace all the (*)'s with <zb15>(*)<z$b$>'s in mevar, wouldn't it?
its always worked for me

doood> basic format is:
$replace =~ s/find_this/replace_with_this/gi;

puttin differant letters at the end has differant effects:

g: find and rerplace all occurances
i: ignore the case
o: replace the first one only
if you use a perl speicla charactor within the serach and replace you need to escape it out (using \)
for expample if you wanted to search and replace all the $'s you'd need to use \$ instead of $ in the code
Make sense?

Siberdude
siberdude@settlers.co.uk
 
The \Q and \E will escape ANYTHING that needs escaping between them, without you having to worry about what needs escaping and what doesn't. It also makes the substitute statement look a LOT neater without all the extra backslashes, and makes it MUCH clearer what you are attempting to accomplish. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
That wasn't meant to be aggression, just emphasis. If you look at the two regular expressions you'll see that the one with \Q and \E really is a lot clearer and easier to understand than the one with all the backslashes in it. Not to mention what would happen if you forgot to escape something that should have been (or used | instead of \, as you did in front of the first $). Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
lol that was jsut a typo... would have been obvios enough when i ran it and it told me &quot;operator found where bareword expecting at line ###&quot;
:p
Its fair enough, i've always used multiple \'s.. but u know what they say about something never everyday
Siberdude
siberdude@settlers.co.uk
 
I still use backslashes when there's just a few characters, but when there's lots of them, like in this case, it's a lot easier to use the quoting metacharacters.

I know, it really wasn't fair to pick on the typo, but I couldn't resist. :) Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top