LinuxQuestions.org
Visit Jeremy's Blog.
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, 05: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, 08:54 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:43 PM
search/replace in many files allelopath Linux - General 1 08-02-2005 10:21 PM
search and replace C code vexer Programming 3 05-19-2004 12:11 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 10:01 AM


All times are GMT -5. The time now is 08:10 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration