473,734 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ 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 1568
* 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`#~t vc!Xibu)%*|qsjo u#Kvtu!A`#~tvc! KBQI!)*|~
tvc!ifmm)%*|#Qf sm!A`#~tvc!jt)% *|(Ibdlfs(~ # What the hell is JAPH? ;
@_=split/\s\s+#/;$_=(join''=>ma p{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
2871
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 http://starship.skyport.net/crew/amk/regex/regex-to-re.html http://www.python.org/doc/howto/regex-to-re/ http://www.amk.ca/python/howto/regex-to-re/ Thanks in advance.
75
4660
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 functions are simplified versions of the full featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form UNQUOTE
3
5305
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 of what I have tried... string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf"; while(true) { Regex .Match(testString,@"(\w)"); } ---------------------------------------------------------------------- string testString = "lkf...
3
5270
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 "<stdin>", line 1, in ? File "/usr/lib/python2.3/sre.py", line 179, in compile return _compile(pattern, flags) File "/usr/lib/python2.3/sre.py", line 230, in _compile
8
2242
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 is the precise terms of license of the official python documentation? The official python.org doc site is not clear. Note also, that the regex syntax used by Perl is the same as Python.
10
3888
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 is using Python's standard re (regular expressions) module to parse the input file. As Python's routines to read from the file and regular expressions are most likely implemented via native libraries I would expect that the C code, which reads...
2
2414
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, in compile return _compile(pattern, flags) File "C:\Python25\lib\re.py", line 233, in _compile
0
1627
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 an '@'in the filename on the assumption it's already in the correct format. Uncomment the os.rename line
4
2142
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
8946
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
8776
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
9449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9236
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
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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
4550
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...
2
2724
muto222
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.