473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Bug in re module?

Ant
Look at the following minimal example:
>>import re
p = re.compile(r"(:?Test) (String)")
m = p.search("This is a Test String OK?")
m.groups()
('Test', 'String')

I would have expected this to produce:

('String')

since (:?...) should be a non-capturing group. From the module
reference:

(?:...)
A non-grouping version of regular parentheses. Matches whatever
regular expression is inside the parentheses, but the substring matched
by the group cannot be retrieved after performing a match or referenced
later in the pattern.

Oct 6 '06 #1
8 999
Ant

Ant wrote:
Look at the following minimal example:
.... (snip example that shows non-capturing group capturing)

Note I get the same results from python versions 2.4 and 2.5.

Oct 6 '06 #2
In article <11**********************@k70g2000cwa.googlegroups .com>,
"Ant" <an****@gmail.comwrote:
Ant wrote:
Look at the following minimal example:
... (snip example that shows non-capturing group capturing)

Note I get the same results from python versions 2.4 and 2.5.
Try ?: instead of :?

Just
Oct 6 '06 #3

Ant wrote:
Look at the following minimal example:
>import re
p = re.compile(r"(:?Test) (String)")
Bzzzzzt! Sorry, PBKAC.

The correct syntax is (?:foo)
You have (:?foo)
which matches an optional colon followed by foo.

Now quick kill your post before the effbot spots it :-)

>m = p.search("This is a Test String OK?")
m.groups()
('Test', 'String')

I would have expected this to produce:

('String')

since (:?...) should be a non-capturing group. From the module
reference:

(?:...)
A non-grouping version of regular parentheses. Matches whatever
regular expression is inside the parentheses, but the substring matched
by the group cannot be retrieved after performing a match or referenced
later in the pattern.
Oct 6 '06 #4
Ant wrote:
>
I would have expected this to produce:

('String')

since (:?...) should be a non-capturing group. From the module
reference:

(?:...)
(?:...) isn't the same thing as (:?...), though.

</F>

Oct 6 '06 #5
Ant

Just wrote:
Try ?: instead of :?
Duh. Put it down to Friday afternoon!

:-\

Don't know what I was thinking that something as high profile as that
could slip through the net!!

Oct 6 '06 #6
Ant

John Machin wrote:
Now quick kill your post before the effbot spots it :-)
Too late - the post was 3 minutes ago you know ;-)

Oct 6 '06 #7
Ant wrote:
John Machin wrote:
>Now quick kill your post before the effbot spots it :-)

Too late - the post was 3 minutes ago you know ;-)
Must be your lucky day - he did spot it, and you're still alive !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 6 '06 #8
Ant wrote:
John Machin wrote:
>Now quick kill your post before the effbot spots it :-)

Too late - the post was 3 minutes ago you know ;-)
+1 QOTW, BTW

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 6 '06 #9

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

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
8
by: Irmen de Jong | last post by:
What would be the best way, if any, to obtain the bytecode for a given loaded module? I can get the source: import inspect import os src = inspect.getsource(os) but there is no...
5
by: dody suria wijaya | last post by:
I found this problem when trying to split a module into two. Here's an example: ============== #Module a (a.py): from b import * class Main: pass ============== ==============
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
40
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
6
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...

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.