LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2005, 04:51 PM   #1
Accordion
LQ Newbie
 
Registered: Nov 2003
Location: Edmonton, Alberta, Canada
Distribution: Slackware 9.1
Posts: 23

Rep: Reputation: 15
Python search and replace


I'm trying to use python to do a simple search and replace within STAX (http://staf.sourceforge.net/current/staxug.pdf) My code is:

<script>
myVar = "GrunthosTheFlatulent"

import re
re.sub('u', 'Q', myVar)
</script>

<call function="'STAXUtilLogAndMsg'">
'myVar is now %s' %(myVar)
</call>


I expected to get GrQnthosTheFlatQlent but got GrunthosTheFlatulent instead. Can someone tell me what I'm doing wrong? Thanks!
 
Old 02-22-2005, 07:54 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
strings are immutable (you can't change them). You have to say myVar = re.sub('u', 'Q', myVar). The difference is that this make myVar refer to a new object (in terms of C, it changes the value of the pointer), whereas mutating methods (such as foo.append(bar)) changes the contents of the object that foo refers to (the object that the pointer points at).

Hope this helps,

Jonas
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
search and replace C code vexer Programming 3 05-18-2004 11:11 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration