regex \b behaviour in python

W

Walter Cruz

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
 
A

André Malo

* Walter Cruz said:
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
 
M

MRAB

* Walter Cruz said:
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:
['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'?
 
W

Walter Cruz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top