473,804 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Begineer Question : Global string substitution with re

Hi,
This is a basic question but I can't figure out what is wron - even after
reading the documentation. I have a script that normalizes strings. One of
the steps is to convert all fractions too the tag 'fraction'. For example :

import re
line = "This is the first ratio, 170/37, and this is the second 170/37 "
def normalise(text) :

#Tag fractions
fraction = r'(\s+\d+\/\d+\s+)'
regfr = re.compile(frac tion)
text = regfr.sub(" |fraction| ",text)

#Remove punctuation
punc = r'\,'
regpunc = re.compile(punc )
text = regpunc.sub("", text)

return text

print line,"\n"
print normalise(line) ,"\n"
The output from this script is :

This is the first ratio, 170/37, and this is the second 170/37

This is the first ratio 170/37 and this is the second |fraction|
I can't understand why only one of the fractions gets substituted. The
documentation for sub states that the default argument for sub is 0 which
means replace all occurences. The output of my script should be :

This is the first ratio |fraction| and this is the second |fraction|
Any help appreciated.

Peter

_______________ _______________ _______________ _______________ _____
Add MSN 8 Internet Software to your existing Internet access and enjoy
patented spam protection and more. Sign up now!
http://join.msn.com/?page=dept/byoa
Jul 18 '05 #1
0 2770

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6049
by: Mark | last post by:
hello! normally, if you are given binary data in a string (such as from a call to fread), you can call the strlen function on this string to get its size: $buffer = fread($file, 100000); if (strlen($buffer) < 100000) { echo "read less than 100000 bytes"; }
3
5012
by: RiGGa | last post by:
Hi, I am trung to create a mysql query string that contais two variables, the first holds a table name and the second holds the values as a tuple. I have tried the following however I can not work out how to get the format right so the %s is subsituted with the contents of the variable, I think I just have the quoting wrong, can anyone advise? tablename contains the table I want to use datavalue contains the data I want to use...
4
1492
by: Chris Bray | last post by:
Hello all, I am trying to globally replace all namespace URIs in an XML document. I have tried using XmlNamespaceManager to do this, but have not had any success. I don't want to do a simple string replacement on the original text, as the URI may be referenced in a text node, which I would not want to replace. Short of regenerating the document node by node, is there any way to do this? Thanks!
12
4649
by: John Leslie | last post by:
I need to write a string to a file in EBCDIC. Do I need to do it character by character using a translation table, or is there a function to translate the whole string? (I am aware that I can convert a whole file using Unix utilities, but this file will have only a few header records in EBCDIC)
4
1545
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an error tells me something like "Additional information: Caractère de substitution faible trouvé...
5
4422
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
21
2318
by: Hitesh | last post by:
Hi, I get path strings from a DB like: \\serverName\C:\FolderName1\FolderName2\example.exe I am writing a script that can give me access to that exe file. But problem is that string is not universal path, I need to add C$. Any idea how I can add $ char in that string. ServerName is not fixed length. It could be any chars length.
8
2679
by: bim_bom | last post by:
Hi, is there any tool to change naming convention in c++ sources? I mean something, that parses cpp and h files in my project, and it finds, what variables are there declared. I think, it should have a public "CALLBACK" function (which would be impemented by me), which would tell the program, how to rename variables and functions. It should work like that: I have sourcecode:
6
6337
by: Generic Usenet Account | last post by:
I was extremely surprised to learn that the extremely rich C++ string API does not have even a single menthod devoted to string substitution i.e. given a string, replace all instances of pattern-1 in the string with pattern-2. There are API methods for finding and replacing, but none on pattern substitution. Although I have developed an implementation for this (posted to the comp.sources.d newsgroup), does anyone have the background why...
2
1305
by: nimbalkar | last post by:
I am the begineer of the .net i know the c,c++,vb please send me notes for the begineer how to and where to write the programme .
0
9716
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
9595
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
10101
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...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
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...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.