473,466 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with newlines in regexp substitution

See the following results:

Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
import re
s = "1"
re.sub('1','\\n',s) '\n' '\\n' '\\n' re.sub('1',r'\\n',s) '\\n' s.replace('1','\\n') '\\n' repl = '\\n'
re.sub('1',repl,s) '\n' s.replace('1',repl)

'\\n'

Why is the behaviour of the regexp substitution so weird and can I prevent
that? It breaks my asumptions and thus my code.

Regards,
Florian Schulze

Feb 23 '06 #1
1 1069
Florian Schulze wrote:
See the following results:

Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
import re
s = "1"
re.sub('1','\\n',s)
'\n'
'\\n'
'\\n'
re.sub('1',r'\\n',s)
'\\n'
s.replace('1','\\n')
'\\n'
repl = '\\n'
re.sub('1',repl,s)
'\n'
s.replace('1',repl)


'\\n'

Why is the behaviour of the regexp substitution so weird and can I
prevent that? It breaks my asumptions and thus my code.

Regards,
Florian Schulze


"Why" questions are always tough to answer. E.g.: Why are we here?

The answer to "what is happening" is much easier. Strings passed to the
regex engine are processed first, so escapes must be escaped. This is
why raw strings were invented. If it weren't for these, I'd still be
using perl. In raw strings, as you have noticed, a '\' is already
escaped. In the olden days, you'd have to type "\\\\" to mean a literal
backslash, so creating a literal backslash in a regex that produced a
string that would then itself be used in a regex would be
'\\\\\\\\\\\\\\\\', which scared me away from Python for a couple of
years (rmember, the final printed product would be '\').

That patently doesn't answer your question, but here is something to ponder:

py> s.replace('1',repl)[0]
'\\'
py> print s.replace('1',repl)
\n

James
Feb 23 '06 #2

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

Similar topics

6
by: Lukas Holcik | last post by:
Hi Python crazies!:)) There is a problem to be solved. I have a text and I have to parse it using a lot of regular expressions. In (lin)u(ni)x I could write in bash: cat file | sed 's/../../' |...
2
by: AlexS | last post by:
Hello, I have error when reading schema using XmlSchema. Read and then .Compile: System.Xml.Schema.XmlSchemaException: May not be nominated as the {substitution group affiliation} of any...
6
by: papa.coen | last post by:
Hi, I need to split/match the following type of (singleline) syntax on all commas (or text in between) that are not between qoutes: A,'B,B',C,,'E',F The text between quotes can be _any_ text...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
3
by: lex __ | last post by:
I'm tryin to use regexp to replace multi-line c-style comments (like /* this /n */ ) with /n (newlines). I tried someting like re.sub('/\*(.*)/\*' , '/n' , file) but it doesn't work for...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
6
by: Pat | last post by:
I have a regexp in Perl that converts the last digit of an ip address to '9'. This is a very particular case so I don't want to go off on a tangent of IP octets. ( my $s = $str ) =~...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
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
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.