473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

search and replace with variable value

I've to search for a pattern and replace with input variable.
Suppose the strings are:

tempSearch : = '111-452-05'
tempSearch:= '111-452-10'
tempSearch:='111-459-15'
tempSearch: ='111-452-20'
tempSearch:='111-452-25'

search for the whole pattern starting from the "temp" to the end "'". I've to replace the value that is occuring after the first appearance of "-" with a user input value.

How can I do it?

Thank You!
Mar 11 '14 #1
3 1500
bvdet
2,851 Expert Mod 2GB
Using str methods only:
Expand|Select|Wrap|Line Numbers
  1. >>> s = "tempSearch : = '111-452-05'"
  2. >>> userStr = "XYZ"
  3. >>> s[:s.index("-")+1]+userStr
  4. "tempSearch : = '111-XYZ"
  5. >>> 
Using re and str methods:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> s = "tempSearch : = '111-452-05'"
  3. >>> userStr = "XYZ"
  4. >>> patt = re.compile(r"-(.+)")
  5. >>> m = patt.search(s)
  6. >>> s.replace(m.group(1), userStr)
  7. "tempSearch : = '111-XYZ"
  8. >>> 
Mar 11 '14 #2
dwblas
626 Expert 512MB
Take a look at "Searching Text" and "Replacing Text" at http://www.freenetpages.co.uk/hp/alan.gauld/tuttext.htm You can also split on the "-" if that is easier to understand.
Expand|Select|Wrap|Line Numbers
  1. testing="tempSearch : = '111-452-05'"
  2. replaced = "new part"
  3. parts = testing.split("-")
  4. print "parts =", parts
  5. parts[1]=replaced
  6. print "-".join(parts) 
Mar 11 '14 #3
Thanks for the reply guys!!!
Mar 17 '14 #4

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

Similar topics

4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
0
by: DaveJG | last post by:
I have a couple of questions involving the search/replace in the IDE and regular expressions. Any help would be greatly appreciated. 1. How would I replace two line feeds with one? That is ...
3
by: Helpseeker | last post by:
Hi all, I have written a small code in which i declare a static int variable and increment its value by one each time i click on a button. actually i use the int variable value in a particular URL...
6
by: paul | last post by:
HI! How do we send a variable from an Iframe page back to its parent? I have a script that calculates the iframe's window size but I need to know how to send that value back to its parent so I...
2
by: Michael Peters | last post by:
is there a way to replace a certain sequence of characters by line feed (vbCrLf ), for all text columns in a table, using Search+Replace? -Michael
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
2
by: duancg | last post by:
Hi, I wonder if someone could help since I wasn't able to find the answer through search. I have a simple .aspx page that shows data from a database table, as a table in UI. Now the data uses...
2
chathura86
by: chathura86 | last post by:
hi, i'm retrieving a string from a database and i want to insert a value which comes from another variable into that. for a example String in the data base: "Dear %name%, Thank you" and i...
3
by: Compufix | last post by:
I am trying to edit a file in place.....it is the /etc/master.passwd in OSX I need to push out a script that adds a crypt password for root. The file by default has an * so the line starts with ...
2
by: jvskarthick | last post by:
Hi, I have a variable and it’s used in my all stored procedure. I want to replace the variable value with new one. Is there a way we can replace the variable with some other one in all the SP and...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.