This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Special sequences \A and \Z don't work in character set []
Type: behavior Stage:
Components: Regular Expressions Versions: Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: re pattern r"[\A]" should work like "A" but matches nothing. Ditto B and Z.
View: 13899
Assigned To: Nosy List: ezio.melotti, georg.brandl, loewis, mrabarnett, py.user
Priority: normal Keywords:

Created on 2012-03-09 06:11 by py.user, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg155208 - (view) Author: py.user (py.user) * Date: 2012-03-09 06:11
>>> import re
>>> re.search(r'\Ac\Z', 'c')
<_sre.SRE_Match object at 0xb74cff38>
>>> re.search(r'[\A]c[\Z]', 'c')
>>> re.purge()
>>> re.search(r'[\A]c[\Z]', 'c', re.DEBUG)
in 
  at at_beginning_string
literal 99 
in 
  at at_end_string
>>>
msg155210 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-03-09 07:37
What behavior would you expect?
msg155253 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2012-03-09 18:10
Within a character set \A and \Z should behave like, say, \C; in other words, they should be the literals "A" and "Z".
msg155273 - (view) Author: py.user (py.user) * Date: 2012-03-10 00:19
Martin v. Löwis wrote:
> What behavior would you expect?

I expected similar work

>>> re.search(r'[\s]a', ' a').group()
' a'
>>> re.search(r'[\s]a', 'ba').group()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> 
>>> 
>>> re.search(r'[^\s]a', ' a').group()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.search(r'[^\s]a', 'ba').group()
'ba'
>>>
msg155276 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2012-03-10 01:09
\s matches a character, whereas \A and \Z don't. Within a character set \s makes sense, but \A and \Z don't, so they should be treated as literals.
msg155294 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-03-10 06:55
Duplicate of #13899.
History
Date User Action Args
2022-04-11 14:57:27adminsetgithub: 58445
2012-03-10 07:17:32georg.brandlsetstatus: open -> closed
resolution: duplicate
2012-03-10 06:55:20georg.brandlsetsuperseder: re pattern r"[\A]" should work like "A" but matches nothing. Ditto B and Z.

messages: + msg155294
nosy: + georg.brandl
2012-03-10 01:09:36mrabarnettsetmessages: + msg155276
2012-03-10 00:19:07py.usersetmessages: + msg155273
2012-03-09 18:10:43mrabarnettsetmessages: + msg155253
2012-03-09 07:37:51loewissetnosy: + loewis
messages: + msg155210
2012-03-09 06:11:13py.usercreate