473,609 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

search and replace with variable value

16 New Member
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:='11 1-459-15'
tempSearch: ='111-452-20'
tempSearch:='11 1-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 1519
bvdet
2,851 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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
nischalinn
16 New Member
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
10190
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 filter() { var items = new Array("John", "Jane");
0
1198
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 Var1 = 1 Var2 = 2 becomes
3
2530
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 so that i can browse to diff URL each time i click the button. Hope you understand the scenario. So here my question is how long is the value of the static variable retained from the time i click on the button, because what i observe is when the...
6
9372
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 can use it there. Thanks in advance :) Paul
2
5519
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
5396
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 example first:
2
3380
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 1~12 to represent months, but I want to show them as Jan/.../Dec. So I thought I could write a simple function to convert it and call that function in the Bind expression of DataList. However, I just couldn't find a way to pass the bind...
2
4381
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 should be able to replace the %name% with a variable value and i also need to have the ability to change the possessions.(just by changing the string in my database).
3
5520
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 root:*:blah blah blah I can read that in using cat and awk and define the old variable, and I can define the new variable using htpasswd but when I try the search and replace I can't get rid of the darn *...I would guess it has something to do...
2
1748
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 recompile it using the Query. Thanks, JK
0
8115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8053
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8557
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8513
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8380
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4007
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1638
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.