473,471 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regex \b behaviour in python

Hi all!

Just a simple question about the behaviour of a regex in python. (I
discussed this on IRC, and they suggest me to post here).

I tried to split the string "walter ' cruz" using \b .

In ruby, it returns:

irb(main):001:0>"walter ' cruz".split(/\b/)
=["walter", " ' ", "cruz"]

and in php:

Array
(
[0] =>
[1] =walter
[2] = '
[3] =cruz
[4] =>
)
But in python the behaviour of \b is differente from ruby or php.

The guys on the IRC pointed me a way to do that: [m.span() for m in
re.finditer(r'\b',"walter ' cruz")], but if fact there's some
differente as it strips the spaces :)

My question is: why \b behaves like this on python? Why it's different
from ruby or php (or even perl, I believe)?

(For the sake of curiosity, I was trying to solve the
http://www.rubyquiz.com/quiz76.html in python. But the question to not
to solve the quiz, but understand why python is different)

[]'s
- Walter
Jun 27 '08 #1
3 1546
* Walter Cruz wrote:
irb(main):001:0>"walter ' cruz".split(/\b/)
=["walter", " ' ", "cruz"]

and in php:

Array
(
[0] =>
[1] =walter
[2] = '
[3] =cruz
[4] =>
)
But in python the behaviour of \b is differente from ruby or php.
My python here does the same, actually:

$ cat foo.py
import re

x = "walter ' cruz"
s = 0
r = []
for m in re.finditer(r'\b', x):
p = m.start()
if s != p:
r.append(x[s:p])
s = p

print r

$ python2.4 foo.py
['walter', " ' ", 'cruz']
$ python2.5 foo.py
['walter', " ' ", 'cruz']
$

nd
--
$_=q?tvc!uif)%*|#Bopuifs!A`#~tvc!Xibu)%*|qsjou#Kvt u!A`#~tvc!KBQI!)*|~
tvc!ifmm)%*|#Qfsm!A`#~tvc!jt)%*|(Ibdlfs(~ # What the hell is JAPH? ;
@_=split/\s\s+#/;$_=(join''=>map{chr(ord( # AndréMalo ;
$_)-1)}split//=>$_[0]).$_[1];s s.*s$_see; # http://pub.perlig.de/ ;
Jun 27 '08 #2
On Jun 19, 8:46*pm, André Malo <auch-ic...@g-kein-spam.comwrote:
* Walter Cruz wrote:
irb(main):001:0>"walter ' cruz".split(/\b/)
=["walter", " ' ", "cruz"]
and in php:
Array
(
* * [0] =>
* * [1] =walter
* * [2] =*'
* * [3] =cruz
* * [4] =>
)
But in python the behaviour of \b is differente from ruby or php.

My python here does the same, actually:

$ cat foo.py
import re

x = "walter ' cruz"
s = 0
r = []
for m in re.finditer(r'\b', x):
* * p = m.start()
* * if s != p:
* * * * r.append(x[s:p])
* * * * s = p

print r

$ python2.4 foo.py
['walter', " ' ", 'cruz']
$ python2.5 foo.py
['walter', " ' ", 'cruz']
$
Another way is:
>>re.split(r"(\W+)", "walter ' cruz")
['walter', " ' ", 'cruz']

\W+ matches the non-word characters and the capturing parentheses
causes them also to be returned.

I'm surprised that splitting on \b doesn't work as expected, so it
might be that re.split has been defined only to split on one or more
characters. Is it something that should it be 'fixed'?
Jun 27 '08 #3
I'm surprised that splitting on \b doesn't work as expected, so it
might be that re.split has been defined only to split on one or more
characters. Is it something that should it be 'fixed'?
Thats's my main doubt: is this a bug or not?

[]'s
- Walter
Jun 27 '08 #4

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

Similar topics

4
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
3
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples...
3
by: gisleyt | last post by:
I'm trying to compile a perfectly valid regex, but get the error message: r = re.compile(r'(*)(\d{1,3}\.\d{0,2})?(\d*)(\,\d{1,3}\.\d{0,2})?(\d*)?.*') Traceback (most recent call last): File...
8
by: Xah Lee | last post by:
the Python regex documentation is available at: http://xahlee.org/perl-python/python_re-write/lib/module-re.html Note that, i've just made the terms of use clear. Also, can anyone answer what...
10
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
2
by: voxiac | last post by:
Could someone tell me why: Fails with message: Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> re.compile('\\dir\\(file)') File "C:\Python25\lib\re.py", line 180,...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
4
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
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
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,...
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.