473,395 Members | 1,474 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,395 software developers and data experts.

Why not match.expand('\\0')?

I'm using the expand method of re MatchObjects, and was surprised to
find that the references don't work with group 0...
import re
p = re.compile('foo')
m = p.match('foo')
m.group(0) 'foo' m.expand('\\0')

'\x00'

Is this intended behavior? Why should group 0 behave differently from
the others?

--
Magnus Lie Hetland "The mind is not a vessel to be filled,
http://hetland.org but a fire to be lighted." [Plutarch]
Jul 18 '05 #1
2 1622
Magnus Lie Hetland wrote:
I'm using the expand method of re MatchObjects, and was surprised to
find that the references don't work with group 0...
import re
p = re.compile('foo')
m = p.match('foo')
m.group(0) 'foo' m.expand('\\0')

'\x00'

Is this intended behavior? Why should group 0 behave differently from
the others?


because there is no group 0; the m.group(0) call is just a shortcut
to get the entire match.

(try m.groups() if you don't believe me)

</F>


Jul 18 '05 #2
In article <ma*************************************@python.or g>,
Fredrik Lundh wrote:
Magnus Lie Hetland wrote:
[snip]because there is no group 0; the m.group(0) call is just a shortcut
to get the entire match.

(try m.groups() if you don't believe me)
Hehe. I believe you -- and I know how this behaves. I guess I'm
questioning the behavior. You say "there is no group 0", while I say
"why isn't group 0 in m.groups(), m.groupdict() and so on?"

In other words, I see "group 0" as a name for the entire match, just
as m.group(0) is a shortcut to it. It clearly exists, in some sense,
but whether it is actually a group or not -- well, I'm quite sure you
are right in the matter. (IIRC, other languages such as AWK don't
treat this as a "short-cut", but as a real group in its own right...
But I may not RC :)

I guess I just think it would be useful to extend the "shortcut" of
the "group" 0 to more than just the group method. There are (IMO)
cases where referencing the entire match (e.g. as '\\0') can be
useful. Of course one can add an extra set of parentheses, but in my
case that's not really feasible.

As it is, I have simply written a wrapper function to deal with the
special case. But I was pretty confused about my unit test output for
a while there...
</F>


--
Magnus Lie Hetland "The mind is not a vessel to be filled,
http://hetland.org but a fire to be lighted." [Plutarch]
Jul 18 '05 #3

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

Similar topics

1
by: elmlish | last post by:
Hello all, I'm currently befuddled as to how to efficiently test for a positive re. match then use the results of that match in a function. Mostly what I've seen people do is to first test for...
3
by: bdwise | last post by:
I have this in my body tag: something();something(); document.thisForm.textBox1.focus();something(); And I want to find a part between the semicolons that ends in focus() and remove the...
6
by: Matt Wette | last post by:
Over the last few years I have converted from Perl and Scheme to Python. There one task that I do often that is really slick in Perl but escapes me in Python. I read in a text line from a file...
6
by: Duane Morin | last post by:
I've inherited an XSL transform that I need to squeeze every last millisecond out of (since it's running several hundred thousand times). I've noticed that there are 26 match clauses in the file....
19
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
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...
0
by: hwait | last post by:
I've a string (got it via WebRequest): <td class=maintable noWrap bgcolor=#666666><b><font color=#ffffff>HDR A </font></b></td> .... Inner text A .... <td class=maintable noWrap...
5
by: JackRazz | last post by:
Anyone know the regular expression to match a blank line where the byte sequence is "0D 0A 0D 0A" ive tried "\r\n\r\n+", "^$+" "\n\r" with no success. Any Ideas? Thanks - JackRazz This is...
6
by: EXI-Andrews, Jack | last post by:
the '*' and '+' don't seem to be greedy.. they will consume less in order to match a string: ('aa', 'ab') this is the sort of behaviour i'd expect from '(a+?)(ab)' a+ should greedily...
4
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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,...
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...
0
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...

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.