Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: String Replace only if whole word?

Reply
Thread Tools

RE: String Replace only if whole word?

 
 
Michael Yanowitz
Guest
Posts: n/a
 
      11-17-2006


-----Original Message-----
From: Carsten Haese [(E-Mail Removed)]
Sent: Friday, November 17, 2006 11:03 AM
To: Michael Yanowitz
Cc: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: RE: String Replace only if whole word?


On Fri, 2006-11-17 at 10:43 -0500, Michael Yanowitz wrote:
> >Your pattern would be "[^A-Za-z0-9_]word[^A-Za-z0-9_]"
> >
> >--
> >Juho Schultz

>
>
> Thanks.
> This works great except for one thing:
>
> The character after the replacement is deleted, so that if I have
> send_data (LAPTOP, test_string)
> it would replace it with:
> send_data (10.1.3.4 test_string)
> (ignoring that the 10.1.3.4 is not quoted). The comma is missing.


If you actually wanted to use string replacement, you'd need a pattern
with look-behind and look-ahead assertions along the lines of
"(?<![A-Za-z0-9_])word(?![A-Za-z0-9_])".

Then again, I don't think string replacement is the right tool for the
job. You're saying that the file you are operating on in this fashion is
a Python script. Why don't you just place something like
LAPTOP="10.1.3.4" at the top of that file, or have the script read this
information from a configuration file?

-Carsten

Thanks, works great (so far).
Yeah, I am operating this on a Python script. However, I am working off
a requirement that the script be pre-processed and the strings replaced
before executing the script and that if there are any remaining (not
replaced)
names that I don't execute the script and report that some 'mnemonics' have
not been replaced.


Thanks:
Michael Yanowitz


 
Reply With Quote
 
 
 
 
Leonhard Vogt
Guest
Posts: n/a
 
      11-19-2006
Michael Yanowitz schrieb:
> Yeah, I am operating this on a Python script. However, I am working off
> a requirement that the script be pre-processed and the strings replaced
> before executing the script and that if there are any remaining (not
> replaced)
> names that I don't execute the script and report that some 'mnemonics' have
> not been replaced.


If you control the source(template), you could use string formatting.
http://docs.python.org/lib/typesseq-strings.html

You would have to rewrite your MNEMONIC as %(MNEMONIC)s .

An exception will be raised if you miss a replacement and you are sure
you do not replace anything by accident.

Leonhard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Win32::TieRegistry -- replace string in the whole HKLM MoshiachNow Perl Misc 0 11-21-2006 09:57 AM
Re: String Replace only if whole word? Tim Chase Python 0 11-17-2006 04:12 PM
RE: String Replace only if whole word? Carsten Haese Python 0 11-17-2006 04:03 PM
String Replace only if whole word? Michael Yanowitz Python 1 11-17-2006 01:38 PM
*WITHOUT* using: ValidateRequest="False" for the whole page (or my whole site).... \A_Michigan_User\ ASP .Net 2 08-21-2006 02:13 PM



Advertisments