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

Home Posts Topics Members FAQ

Strange re behavior: normal?

How is re.split supposed to work? This wasn't at all what I expected:

[rmunn@localhost ~]$ python
Python 2.2.2 (#1, Jan 12 2003, 12:07:20)
[GCC 3.2] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
import re
re.split(r'\W+' , 'a b c d') ['a', 'b', 'c', 'd'] # Expected result. .... re.split(r'\b', 'a b c d') ['a b c d'] # Huh?
Since \b matches the empty string, but only at the beginning and end of
a word, I would have expected re.split(r'\b', 'a b c d' to produce
either:

['', 'a', ' ', 'b', ' ', 'c', ' ', 'd', '']

or:

['a', ' ', 'b', ' ', 'c', ' ', 'd']

But I didn't expect that re.split(r'\b', 'a b c d') would yield no splits
whatsoever. The module doc says "split(patt ern, string[, maxsplit = 0]):
split string by the occurrences of pattern". re.findall() seems to think
that \b occurs eight times in 'a b c d':
re.findall(r'\b ', 'a b c d')

['', '', '', '', '', '', '', '']

So why doesn't re.split() think so? I'm puzzled.

--
Robin Munn <rm***@pobox.co m> | http://www.rmunn.com/ | PGP key 0x6AFB6838
-----------------------------+-----------------------+----------------------
"Remember, when it comes to commercial TV, the program is not the product.
YOU are the product, and the advertiser is the customer." - Mark W. Schumann
Jul 18 '05 #1
5 1525
Robin Munn wrote:
How is re.split supposed to work? This wasn't at all what I expected:

[rmunn@localhost ~]$ python
Python 2.2.2 (#1, Jan 12 2003, 12:07:20)
[GCC 3.2] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
import re
re.split(r' \W+', 'a b c d')
['a', 'b', 'c', 'd']
# Expected result.
...
re.split(r' \b', 'a b c d')
['a b c d']
# Huh?

Since \b matches the empty string, but only at the beginning and end of
a word, I would have expected re.split(r'\b', 'a b c d' to produce
either:

['', 'a', ' ', 'b', ' ', 'c', ' ', 'd', '']

or:

['a', ' ', 'b', ' ', 'c', ' ', 'd']

But I didn't expect that re.split(r'\b', 'a b c d') would yield no splits
whatsoever. The module doc says "split(patt ern, string[, maxsplit = 0]):
split string by the occurrences of pattern". re.findall() seems to think
that \b occurs eight times in 'a b c d':

re.findall( r'\b', 'a b c d')


['', '', '', '', '', '', '', '']

So why doesn't re.split() think so? I'm puzzled.


It looks like re.split is not splitting on zero-length matches. I get
similar behavior (in Python 2.2.2) if I try:

re.split('x*', 'a b c d')

or

re.split('(?=c) ', 'a b c d')

I don't have the Python source handy to verify this hypothesis, though.

If this is correct, it should at least be documented.

David

Jul 18 '05 #2
Fredrik Lundh wrote:
Mike Rovner wrote:
IMHO that split behavior is a bug although technicaly it is not.
(From re manual:
"This module provides regular expression matching operations similar
to those found in Perl.")


is "split" really a matching operation?

fact is, all methods have Python-specific behaviour. it's just the RE
language itself that's based on Perl.


With all due respect to Python and not trying to bend it to any other
language
I believe it trys to do what user expects.

string.split() splits on (clearly documented nonempty) substring.
re.split() splits on RE.
splut(r'\b',... ) clearly means (at least for me) 'split on word boundary'
It doesn't reject it (as in r'\b?') nor provide expected behavior.

I understand why it does what it does, but don't agree with it.

Regards,
Mike


Jul 18 '05 #3
Mike Rovner wrote:
With all due respect to Python and not trying to bend it to any other
language I believe it trys to do what user expects.


it does exactly what I expect it to do.

</F>


Jul 18 '05 #4
"Fredrik Lundh" <fr*****@python ware.com> writes:
Mike Rovner wrote:
With all due respect to Python and not trying to bend it to any other
language I believe it trys to do what user expects.


it does exactly what I expect it to do.

</F>


You don't count. ;-)
John
Jul 18 '05 #5
Michael Janssen <Ja*****@rz.u ni-frankfurt.de> wrote:

What's the good of splitting by boundaries? Someone else wanted this a
few days ago on tutor and I can't figure out a reason by now.


Heh. I bet I know the name of the person who was asking about this on
the tutor list. He's a friend of mine, and I've been helping him learn
Python. He E-mailed me about trying to split on word boundaries with
re.split(r'\b', 'some text'), and it was his E-mail that caused me to
discover that splitting by boundaries didn't do what I expected.

What's the good of it? As someone else pointed out, it allows you to
fetch the words and the separating text, yielding:

['See', ' ', 'Spot', '. ', 'See', ' ', 'Spot', ' ', 'run', '.']

which may be useful in certain English-language-parsing situations,
since it would allow you to look "ahead" or "back" from a word to see
what punctuation precedes or follows it.

Anyway, the re.split behavior I described isn't particularly bothering
me, but I do think it should be better documented. Time to submit a doc
patch, methinks...

--
Robin Munn <rm***@pobox.co m> | http://www.rmunn.com/ | PGP key 0x6AFB6838
-----------------------------+-----------------------+----------------------
"Remember, when it comes to commercial TV, the program is not the product.
YOU are the product, and the advertiser is the customer." - Mark W. Schumann
Jul 18 '05 #6

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

Similar topics

0
1712
by: thulsey | last post by:
Hi all, I've got some strange behavior happening in Firefox and Safari (Khtml and Gecko) that displays *almost* fine in IE6.0 (still trying to get pixels to line up, anal anal anal...) To speed things up, and to avoid any problems with looking through the css file, I'll say first that I based the navigation menu on the "Suckerfish Dropdown menu" online demo over at htmldog. (http://www.htmldog.com/articles/suckerfish/dropdowns/). In...
6
3028
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\" \"\"" << outfilename << "\"\""; //line 75
2
1803
by: Yaro | last post by:
Hi UDB 8.2.2 Win I am little confused about IDENTITY behavior. create table aaa( c1 integer not null generated by default as identity, c2 integer, primary key (c1)
2
2689
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled across some wierd loop behavior. With the pasted code I receive the output that follows. I realize that the code is broken, because the inner loop fails to reset j for each iteration of the outer loop (the fix is commented out). I also know that...
0
328
by: Kris Vanherck | last post by:
yesterday i started getting this strange error when i try to run my asp.net project: Compiler Error Message: CS0006: Metadata file 'c:\winnt\microsoft.net\framework\v1.1.4322\temporary asp.net files\spsweb\0e3514bf\cb1844e7\assembly\dl2\3b163f 16\00452d31_84e5c301\infragistics.webui.ultrawebgrid.v3.dll' could not be found
3
1057
by: George Hardy IV | last post by:
Hello all, I have two forms in my project... one form allows me to use the arrow keys to move and delete controls on my form. the other form does NOT allow me to delete controls or move them with arrow keys, but I can move the controls with the mouse. Nothing is locked on the form, so I am very confused. Anyone know what the deal is? Also, when I have a tab control on the form, and select it, my mouse gets
2
1595
by: KDawg44 | last post by:
Hi, I have the following code: $resultSet = mysql_query($sqlString); $numResults = mysql_num_rows($resultSet); echo "Num Results = " . $numResults . "<br/>"; for ($i = 0; $i < $numResults; $i++) { $row = mysql_fetch_array($resultSet); echo "i = " . $i . "<br/>";
3
3600
by: TC | last post by:
I have a form which I'd like to display as a sizable dialog box. The behavior I get when I use FormBorderStyle = SizableToolWindow is perfect. However, when I use that option, the title bar of my form gets much smaller. The discrepancy really stands out, and looks like a bug. I anticipate that someone will tell me that the different title bar is intentional -- that showing a different title bar is Windows' way of visually distinguishing...
160
5926
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
9711
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
9591
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
10343
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...
1
10331
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6861
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
5529
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
4306
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
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.