You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Confirmed on Pythons 2.1.3, 2.2.3, 2.3.2, and current CVS.
More generally, split() doesn't appear to split on any empty
(0-length) match. For example,
>>> pat = re.compile(r'\b')
>>> pat.split('(a b)')
['(a b)']
>>> pat.findall('(a b)') # but the pattern matches 4 places
['', '', '', '']
>>>
That's probably a design constraint, but isn't documented.
For example, if you split "abc" by the pattern x*, what do you
expect? The pattern matches (with length 0) at 4 places,
but I bet most people would be surprised to get
['', 'a', 'b', 'c', '']
back instead of (as they do get)
['abc']
effbot commented on Dec 11, 2003
effbotmannequin
Mannequin
More actions
Logged In: YES
user_id=38376
Split never splits on empty substrings; see Tim's answer for a
brief discussion.
Fred, can you perhaps add something to the documentation?
mkc commented on Jan 1, 2004
mkcmannequin
Mannequin
More actions
Logged In: YES
user_id=555
Hi, I was going to file this bug just now myself, as this
seems like a really useful feature. For example, I've
several times wanted to split on '^' or '^(?=S)' (to split
up a data file into paragraphs that start with an initial
S). Instead I have to do something like '\n(?=S)', which is
rather more hideous.
To answer tim_one's challenge, yes, I *do* expect splitting
by 'x*' to break a string into letters, now that I've
thought about it. To not do so is a bizarre and surprising
behavior, IMO. (Patient: Doctor, when I split on this
nonsense pattern I get nonsense! Doctor: Then don't do that.)
The fix should be near this line in _sre.c, I think.
if (state.start==state.ptr) {
I could work on a patch if you'll take it...
Mike
jburgy commented on Jan 14, 2004
jburgymannequin
MannequinAuthor
More actions
Logged In: YES
user_id=618572
Since I really needed the functionality described above, I
came up with a broke-around. It's a sufficient replacement,
maybe it belongs in some FAQ:
>>> import re
>>> re.sub('(?im)^$', '\f', 'foo\n\nbar').split('\f')
['foo\n', '\nbar']
Doc note checked in as r57878. Can we conclude based upon Tim's
and Fredrik's comments that this behavior is to be expected and
won't change? If so, I'll close this item.
Well, I think we can conclude that it's expected by *them*. :-) I
still find it surprising, and it somewhat lessens the utility of
re.split for my use cases. (I think re.finditer may also suffer from
the same problem, but I don't recall.)
If you look at the comments attached to the patch for this bug, it
looks like akuchling and rhettinger more or less saw this as being a bug
worth fixing, though there were questions about exactly what the
correct fix should be.
One comment about the your doc fix: You highlight a fairly useless
zero-character match (e.g., "x*") to demonstrate the behavior, which
might leave the user scratching his head. (I think this case was
originally mentioned as a corner case, not one that would be useful.)
It'd be nice to highlight a
more useful case like '^(?=S)' or perhaps a little more generically
something like '^(?=HEADER)' or '^(?=BEGIN)' which is a usage that
tripped me up in the first place.
I'd feel better about this bug being 'wont fix'ed if I had a sense that
several people considered the patch and thought that it sucked. At the
moment, it seems more like it just fell off of the end without ever
being seriously contemplated. :-(
Activity
jburgy commentedon Dec 2, 2003
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200
32 bit (Intel)] on win32
I expect ['foo\n', '\nbar'], since, according to the
documentation $ "in MULTILINE mode also matches
before a newline".
Thanks, Jan
tim-one commentedon Dec 2, 2003
Logged In: YES
user_id=31435
Confirmed on Pythons 2.1.3, 2.2.3, 2.3.2, and current CVS.
More generally, split() doesn't appear to split on any empty
(0-length) match. For example,
That's probably a design constraint, but isn't documented.
For example, if you split "abc" by the pattern x*, what do you
expect? The pattern matches (with length 0) at 4 places,
but I bet most people would be surprised to get
['', 'a', 'b', 'c', '']
back instead of (as they do get)
['abc']
effbot commentedon Dec 11, 2003
Logged In: YES
user_id=38376
Split never splits on empty substrings; see Tim's answer for a
brief discussion.
Fred, can you perhaps add something to the documentation?
mkc commentedon Jan 1, 2004
Logged In: YES
user_id=555
Hi, I was going to file this bug just now myself, as this
seems like a really useful feature. For example, I've
several times wanted to split on '^' or '^(?=S)' (to split
up a data file into paragraphs that start with an initial
S). Instead I have to do something like '\n(?=S)', which is
rather more hideous.
To answer tim_one's challenge, yes, I *do* expect splitting
by 'x*' to break a string into letters, now that I've
thought about it. To not do so is a bizarre and surprising
behavior, IMO. (Patient: Doctor, when I split on this
nonsense pattern I get nonsense! Doctor: Then don't do that.)
The fix should be near this line in _sre.c, I think.
I could work on a patch if you'll take it...
Mike
jburgy commentedon Jan 14, 2004
Logged In: YES
user_id=618572
Since I really needed the functionality described above, I
came up with a broke-around. It's a sufficient replacement,
maybe it belongs in some FAQ:
Another "magic" byte could replace '\f'...
Regards, Jan
mkc commentedon Jul 11, 2004
Logged In: YES
user_id=555
I made a patch that addresses this (bpo-988761).
smontanaro commentedon Sep 1, 2007
Doc note checked in as r57878. Can we conclude based upon Tim's
and Fredrik's comments that this behavior is to be expected and
won't change? If so, I'll close this item.
mkc commentedon Sep 3, 2007
Well, I think we can conclude that it's expected by *them*. :-) I
still find it surprising, and it somewhat lessens the utility of
re.split for my use cases. (I think re.finditer may also suffer from
the same problem, but I don't recall.)
If you look at the comments attached to the patch for this bug, it
looks like akuchling and rhettinger more or less saw this as being a bug
worth fixing, though there were questions about exactly what the
correct fix should be.
http://bugs.python.org/issue988761
One comment about the your doc fix: You highlight a fairly useless
zero-character match (e.g., "x*") to demonstrate the behavior, which
might leave the user scratching his head. (I think this case was
originally mentioned as a corner case, not one that would be useful.)
It'd be nice to highlight a
more useful case like '^(?=S)' or perhaps a little more generically
something like '^(?=HEADER)' or '^(?=BEGIN)' which is a usage that
tripped me up in the first place.
Thanks for working on this!
mkc commentedon Apr 14, 2008
I'd feel better about this bug being 'wont fix'ed if I had a sense that
several people considered the patch and thought that it sucked. At the
moment, it seems more like it just fell off of the end without ever
being seriously contemplated. :-(