473,407 Members | 2,306 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,407 software developers and data experts.

find a given string and added a single character at the end of that string

Hi
I want to find single occurrence of "season ?" where ? is any single digit 1-9 and replace it with season 0? cannot simply replace season with season 0 as there are season 0? which are not to be changed.
Sep 9 '13 #1
1 1207
bvdet
2,851 Expert Mod 2GB
Here's a re solution:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. s = 'I want to find single occurrence of "season 08" where ? is any single digit 1-9 and season 91 season 4 season 5 abcdef'
  4.  
  5. patt = re.compile(r'(season [1-9](?![0-9]))')
  6.  
  7. m = patt.search(s)
  8. if m:
  9.     s = re.sub(patt, 'season 0' + m.group(1)[-1], s, 1)
  10.  
  11. print s
The output:
Expand|Select|Wrap|Line Numbers
  1. >>> I want to find single occurrence of "season 08" where ? is any single digit 1-9 and season 91 season 04 season 5 abcdef
Sep 9 '13 #2

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

Similar topics

8
by: Julia Briggs | last post by:
Hello, trying to create logic tests against a string allowing someone to enter a certain character once or twice into a form consecutively, but not twice seperated between other characters.... ...
2
by: Mike P | last post by:
I want to find a particular character in a string, and then find the length of the string after this point in the string, so that I can do a rightpad on it if it is of a certain length (basically I...
3
by: micmic | last post by:
Deear all experts, In the MySQL, we can use escape character '\' to save the STRING WITH single quote into database (eg. we would like to insert into table "tbl_ABC"with the string ab'c, we...
4
by: Martin Hill | last post by:
Hi there, How can I find the components added to web form from my custom web control? Suppose I added a Timer component (with name "timer1") onto the web form, from my user web control, how...
6
by: | last post by:
How do I convert a single character, e.g. "a" into char for use in the 'split' command? p.s. I have option strict on Tia.
5
by: wanghz | last post by:
Hello, everyone. I have a problem when I'm processing unicode strings. Is it possible to get the 8bit-string representation of any unicode string? Suppose I get a unicode string: a =...
5
by: Martin Kulas | last post by:
Hello! How do I replace a single character in a string at a given position? From programming languages like C I expect something like that: Traceback (most recent call last): File...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
2
by: Konstantinos Pachopoulos | last post by:
Hi, i have the following string s and the following code, which doesn't successfully remove the "\", but sucessfully removes the "\\". .... if i!="\\": .... newS=newS+i .......
2
by: taylordude | last post by:
Hello, I am trying to develop a vb program for Excel which will search through open workbooks for a text string (let's say that string is "error" for example) in a cell. Once this program finds this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.